Search code examples
silverlightwindows-phone-7listboxwindows-phone-7.1panorama-control

Adding datatemplate to listbox and panorama items form program in windows phone 7


I am adding panorama items to panorama controls dynamically, they are adding successfully without any issues

but when i try to add the listbox to panorama item its giving error. I am not able to see the exception also, application automatically closing and I am seeing emulator home screen after that.

Following is my code which I've written to create panorama item and list box

lstAnniversaries = new ListBox()
lstAnniversaries.Width = 420;
                        lstAnniversaries.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                        lstAnniversaries.Foreground = new SolidColorBrush(FacebookQueries.GetColorFromHexString("#000000"));
                        lstAnniversaries.Background = new SolidColorBrush(FacebookQueries.GetColorFromHexString("#ffffff"));
                        lstAnniversaries.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(lstUpcoming_Tap);
                        lstAnniversaries.ItemTemplate = (DataTemplate)XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<Grid  Height=""100"" Margin=""0,0,0,0"">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width=""90"" />
                <ColumnDefinition Width=""210"" />
                <ColumnDefinition Width=""*"" />
            </Grid.ColumnDefinitions>
            <Image Source=""{Binding ImageSource}"" Height=""90"" Width=""90"" Margin=""0,0,11,0"" />
            <StackPanel Grid.Column=""1"" Margin=""0,10,0,0"">
                <TextBlock Text=""{Binding NameSource}"" Style=""{StaticResource ProfileNameStyleForTextBlock}""  />
                <StackPanel Orientation=""Horizontal"" Margin=""0,-2,0,0"">
                    <TextBlock Text=""{Binding EventName}"" Foreground=""#000000"" Style=""{StaticResource EventNameStyleForTextBlock}""   />
                    <TextBlock Text=""{Binding EventDate}"" Foreground=""{Binding EventColor}"" Style=""{StaticResource EventDateStyleForTextBlock}""  />
                </StackPanel>
            </StackPanel>
            <Button VerticalAlignment=""Center"" Height=""Auto""     Name=""btnAnniversary"" Width=""75"" Margin=""5,0,0,0"" HorizontalAlignment=""Left""     Visibility=""{Binding EllipseStatus}"" Tag=""{Binding BindsDirectlyToSource=True}""     Click=""btnAnniversary_Click"" Canvas.ZIndex=""1"" Grid.Column=""2"">
                <Image  Source=""/GiftGiv;component/Assets/bubble.png"" />
            </Button>
        </Grid>
    </DataTemplate>");
                        pan_anniversaries = new PanoramaItem();
                        pan_anniversaries.HeaderTemplate =     (DataTemplate)XamlReader.Load(@"<DataTemplate     xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""     xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""><TextBlock Text=""anniversaries""    FontSize=""54"" Foreground=""Black"" Margin=""-10,0,0,0""></TextBlock></DataTemplate>");

                        Grid grd = new Grid();
                        grd.Children.Add(lstAnniversaries);

                        pan_anniversaries.Content = grd;

                        PanoramaControl.Items.Add(pan_anniversaries);

EDIT:

Output windows Text

A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll
Step into: Stepping over method without symbols 'System.Reflection.RuntimeMethodInfo.InternalInvoke'
Step into: Stepping over method without symbols 'System.Reflection.MethodBase.Invoke'
Step into: Stepping over method without symbols 'System.Delegate.DynamicInvokeOne'
Step into: Stepping over method without symbols     'System.MulticastDelegate.DynamicInvokeImpl'
Step into: Stepping over method without symbols 'System.Delegate.DynamicInvoke'
Step into: Stepping over method without symbols     'System.Windows.Threading.DispatcherOperation.Invoke'
Step into: Stepping over method without symbols 'System.Windows.Threading.Dispatcher.Dispatch'
Step into: Stepping over method without symbols 'System.Windows.Threading.Dispatcher.OnInvoke'
Step into: Stepping over method without symbols 'System.Windows.Hosting.CallbackCookie.Invoke'
Step into: Stepping over method without symbols 'System.Windows.Hosting.DelegateWrapper.InternalInvoke'
Step into: Stepping over method without symbols 'System.Windows.RuntimeHost.ManagedHost.InvokeDelegate'
A first chance exception of type 'System.Exception' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in     System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'System.Exception' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
The thread '<No Name>' (0xf640c32) has exited with code 0 (0x0).
The thread '<No Name>' (0xf450ca6) has exited with code 0 (0x0).
The thread '<No Name>' (0xf050d2e) has exited with code 0 (0x0).
The thread '<No Name>' (0xf820cda) has exited with code 0 (0x0).
The thread '<No Name>' (0xecb0e52) has exited with code 0 (0x0).

Solution

  • Instead of creating DataTemplates in code file, add DataTemplate code in resources section of Page or APP.

    Use following way will resolve the problem as @igrali mentioned in the comments

     lstAnniversaries.ItemTemplate = this.Resources["AnniversariesTemplate"] as DataTemplate;
     lstAnniversaries.Style = this.Resources["EventsListStyle"] as Style;