So I am working on a windows phone app, and I am trying to move elements between two stackpanels (which are basically the two main screens of my app).
I have a pivot item which looks like this:
<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>
When an item in the AllTokenStack is double tapped, I want to move it over to the myTokenStack. When I do that, the program crashes and says “The parameter is incorrect”.
It only does this if I am NOT in debugging mode (so if the phone is unplugged from the computer and I try to run the app). If it is in debugging mode, it works fine.
Here is the code I am using to transfer the object over:
private void container_Tap(object sender, GestureEventArgs e) {
if (AllTokenContainer.Children.Contains(this)) {
AllTokenContainer.Children.Remove(this);
MyTokenContainer.Children.Add(this);
}
}
Does anyone know how to resolve this strange bug?
EDIT
Just to make it clear. The C# code is inside a class which I called Token. The Token class is a user control. It is that control which the user taps to trigger the event. Is this the wrong way to do it?
Stacktrace from the exception:
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)
Rather than using UserControls, I would try to use databinding, with
ObservableCollection‘s of token classes in the back end. Moving things around becomes a little easier when the GUI part gets taken care of by the binding.For an example of how to do this, I created a Windows Phone project using the “Windows Phone Pivot Application” template to use as a base, and named it “TokenAnswer” (if you do this and paste in the code below, you should have a working example).
To MainPage.xaml, I added the DoubleTap event to the first list’s item template, and set the SecondListBox binding to “Items2”. I also set Tag={Binding}, which sets the Tag variable to the ItemViewModel behind the GUI item (this is done so I can access the item tapped, there are other ways to do this, but this one is easy enough for this example).
In the MainViewModel.cs, I added a second collection (“Items2”) and initialized it in the constructor, this collection is for the second listbox:
Finally, in MainPage.xaml.cs, I added the codebehind for the event handler, to remove the item from the first collection, and add it to the second.
Hopefully you can use this as a guide to help your current project!