1

所以我正在开发一个 Windows 手机应用程序,我试图在两个堆栈面板(基本上是我的应用程序的两个主屏幕)之间移动元素。

我有一个如下所示的枢轴项目:

<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 的类中。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)
4

1 回答 1

2

我会尝试使用数据绑定,而不是使用 UserControl,ObservableCollection在后端使用令牌类。当 GUI 部分由绑定处理时,移动事物变得更容易一些。

对于如何执行此操作的示例,我使用“Windows Phone Pivot Application”模板创建了一个 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);
    }

希望您可以将此作为指南来帮助您当前的项目!

于 2012-05-22T21:10:43.747 回答