Question

I have problems adding panorama items through code.

Here is test xaml that works.

<controls:Panorama x:Name="pano">
    <controls:Panorama.Title>...</controls:Panorama.Title>
    <controls:Panorama.Background>...</controls:Panorama.Background>
    <local:TestPanoramaItem ...></local:HoroscopePanoramaItem>
    <local:TestPanoramaItem ...></local:HoroscopePanoramaItem>
    <local:TestPanoramaItem ...></local:HoroscopePanoramaItem>
</controls:Panorama>

TestPanoramaItem is class that inherits from PanoramaItem and in xaml all works fine. Now i want to reorder panorama items through code.

EDIT: I removed items from xaml from now on

I made default order in static string like "pan1,pan2,pan3,..." and on OnNavigatedToHandler (in MainPage) i use code like this:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            String[] order = App.Order.Split(',');
            App.items.Clear();
            foreach (String o in order)
            {
                switch (o)
                {
                    case "pan1": App.items.Add(App.pan1); break;
                    case "pan2": App.items.Add(App.pan2); break;
                    case "pan3": App.items.Add(App.pan3); break;
                    ...
                }
            }
            pano.Items.Clear();

            foreach (TestPanoramaItem ti in App.items)
                pano.Items.Add(ti);
            base.OnNavigatedTo(e);
        }

For the first time called (this is in MainPage) everything looks ok. Panorama works. There I have menu button that navigates to settings where i can change order of items. On OnNavigateFrom handler in settings page i update App.Order static string. After that OnNavigatedTo of main page is called again and everything goes fine (no exceptions) but i cant move panorama and in panorama items layout is broken.

Any help or solutions?

Was it helpful?

Solution

The problem looks to be in part of the code you're not sharing with us.
Try creating the smallest possible complete example which demonstrates the problem.

There also appears to be some confusion between TestPanoramaItem and HoroscopePanoramaItem in the code in your question.

I can dynamically rearrange standard PanoramaItems without issue.
The following is based on the default Panorama template:

Mainpage.xaml:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <controls:Panorama Name="myPano" Title="my application">
        <controls:Panorama.Background>
            <ImageBrush ImageSource="PanoramaBackground.png"/>
        </controls:Panorama.Background>
    </controls:Panorama>
</Grid>

<!--Panorama-based applications should not show an ApplicationBar but this is just for testing-->
<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1" Click="ApplicationBarIconButton_Click"/>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

MainPage.xaml.cs

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        myPano.Items.Clear();

        if (new Random().Next(1, 2) == 1)
        {
            myPano.Items.Add(App.pan1);
            myPano.Items.Add(App.pan2);
            myPano.Items.Add(App.pan3);
        }
        else
        {
            myPano.Items.Add(App.pan1);
            myPano.Items.Add(App.pan3);
            myPano.Items.Add(App.pan2);
        }

        base.OnNavigatedTo(e);
    }

    private void ApplicationBarIconButton_Click(object sender, EventArgs e)
    {
        NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
    }

App.xaml.cs:

public static PanoramaItem pan1
{
    get { return new PanoramaItem { Header = "first item" }; }
}
public static PanoramaItem pan2
{
    get { return new PanoramaItem { Header = "second item" }; }
}
public static PanoramaItem pan3
{
    get { return new PanoramaItem { Header = "third item" }; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top