2012-05-22 44 views
1

因此,我正在一個Windows Phone應用程序,我試圖移動兩個堆疊面板之間的元素(這基本上是我的應用程序的兩個主屏幕)。該參數是不正確的錯誤與堆疊面板

我有一個看起來像這樣一個支點項目:

<controls:Pivot Title="MY APPLICATION"> 
<!--Pivot item one--> 
    <controls:PivotItem Header="All Tokens"> 
     <ListBox x:Name="AllTokenListBox" Margin="0,0,0,0"> 
      <StackPanel x:Name="AllTokenStack"></StackPanel> 
     </ListBox> 
    </controls:PivotItem> 
    <!--Pivot item two--> 
    <controls:PivotItem Header="My Tokens"> 
     <ListBox x:Name="MyTokenListBox" Margin="0,0,0,0"> 
      <StackPanel x:Name="myTokenStack"></StackPanel> 
     </ListBox> 
    </controls:PivotItem> 
</controls:Pivot> 

當AllTokenStack一個項目是雙竊聽,我想將其移動到myTokenStack。當我這樣做時,程序崩潰並說「參數不正確」。 它只有這樣做,如果我不在調試模式(所以如果手機從電腦拔出,我嘗試運行應用程序)。如果它處於調試模式,它工作正常。

這裏是代碼我使用的轉學過來的對象:

private void container_Tap(object sender, GestureEventArgs e) { 
    if (AllTokenContainer.Children.Contains(this)) { 
     AllTokenContainer.Children.Remove(this); 
     MyTokenContainer.Children.Add(this);  
    } 
} 

有誰知道如何解決這個奇怪的錯誤?

編輯 只是爲了說清楚。 C#代碼位於我稱爲Token的類中。令牌類是一個用戶控件。這是用戶點擊觸發事件的控制。這是做錯了嗎?從異常

堆棧跟蹤:

at MS.Internal.XcpImports.CheckHResult(UInt32 hr) 
    at MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection`1 collection, CValue value) 
    at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection`1 collection, DependencyObject value) 
    at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value) 
    at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value) 
    at System.Windows.PresentationFrameworkCollection`1.Add(UIElement value) 
    at MTG_Token_Tracker.TokenGraphic.container_Tap(Object sender, GestureEventArgs e) 
    at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) 
    at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName) 
+1

在這種情況下'這'是什麼,你不想移動發件人? –

+0

如果你有錯誤...發佈異常,而不是消息... –

+0

要發佈異常詳細信息,請將您的container_Tap正文封裝在try/catch(異常e)中,並以某種方式顯示異常(e)(可能是MessageBox。顯示)... –

回答

2

而不是使用用戶控件,我會嘗試在後端使用數據綁定,與ObservableCollection「令牌班秒。當GUI部件被綁定處理時,移動事物會變得更容易一些。

有關如何執行此操作的示例,我使用「Windows Phone數據透視應用程序」模板創建了一個Windows Phone項目作爲基準,並將其命名爲「TokenAnswer」(如果您這樣做並粘貼代碼下面,你應該有一個工作的例子)。

對於MainPage.xaml,我將DoubleTap事件添加到第一個列表的項目模板,並將SecondListBox綁定設置爲「Items2」。我還設置了Tag = {Binding},它將Tag變量設置爲GUI項目後面的ItemViewModel(這樣做是爲了我可以訪問項目,還有其他方法可以做到這一點,但這個例子對於這個例子來說很簡單)。

<phone:PhoneApplicationPage 
x:Class="TokenAnswer.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
SupportedOrientations="Portrait" Orientation="Portrait" 
shell:SystemTray.IsVisible="True"> 

<!--LayoutRoot is the root grid where all page content is placed--> 
<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <!--Pivot Control--> 
    <controls:Pivot Title="MY APPLICATION"> 
     <!--Pivot item one--> 
     <controls:PivotItem Header="first"> 
      <!--Double line list with text wrapping--> 
      <ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel DoubleTap="Token_DoubleTap" Tag="{Binding}" Margin="0,0,0,17" Width="432" Height="78"> 
          <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
          <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </controls:PivotItem> 

     <!--Pivot item two--> 
     <controls:PivotItem Header="second"> 
      <!--Triple line list no text wrapping--> 
       <ListBox x:Name="SecondListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items2}"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Margin="0,0,0,17"> 
           <TextBlock Text="{Binding LineOne}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
           <TextBlock Text="{Binding LineThree}" TextWrapping="NoWrap" Margin="12,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
     </controls:PivotItem> 
    </controls:Pivot> 
</Grid> 

</phone:PhoneApplicationPage> 

在MainViewModel.cs,我增加了第二個集合( 「Items2」),並在構造函數初始化它,這個系列是第二個列表框:

public MainViewModel() 
    { 
     this.Items = new ObservableCollection<ItemViewModel>(); 
     this.Items2 = new ObservableCollection<ItemViewModel>(); 
    } 

    /// <summary> 
    /// A collection for ItemViewModel objects. 
    /// </summary> 
    public ObservableCollection<ItemViewModel> Items { get; private set; } 
    public ObservableCollection<ItemViewModel> Items2 { get; private set; } 

最後,在的MainPage。 xaml.cs中,我添加了事件處理程序的代碼隱藏功能,從第一個集合中刪除該項目,並將其添加到第二個集合中。

private void Token_DoubleTap(object sender, GestureEventArgs e) 
    { 
     var token = (sender as StackPanel).Tag as ItemViewModel; 
     App.ViewModel.Items.Remove(token); 
     App.ViewModel.Items2.Add(token); 
    } 

希望你可以用它作爲指導,幫助你目前的項目!

相關問題