Domanda

I have a problem where my page displays normally when I test against emulator. However, when I run it on my device the page is empty!

I am making a pivot control with an item template, inside the Pivot.ItemTemplate I have a ListBox also with ListBox.ItemTemplate

The code below should produce a page titled 'PIVOT TEST' with 3 pivot items: 'pivot 1', 'pivot 2', 'pivot 3'. Inside each pivot, there should be a list. For 'pivot 1' there should be 3 items in the list: 'name 1', 'name 2', 'name 3'. For 'pivot 2' there should be 2 items in the list: 'name 1', 'name 2'. For 'pivot 3' there should be 1 item in the list: 'name 1'

Here is the xaml:

...
<controls:Pivot x:Name="pivot" Title="PIVOT TEST">
    <controls:Pivot.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding TitleText}" />
        </DataTemplate>
    </controls:Pivot.HeaderTemplate>
    <controls:Pivot.ItemTemplate>
        <DataTemplate>
            <ListBox ItemsSource="{Binding List}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Name}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </DataTemplate>
    </controls:Pivot.ItemTemplate>
</controls:Pivot>
...

And here is the code for the whole page:

public partial class PivotTest : PhoneApplicationPage {
    private List<RandomObject> randomObjectList 
        = new List<RandomObject>();

    public PivotTest() {
        InitializeComponent();

        randomObjectList.Add(new RandomObject() {
            Name = "name 1"
        });
        randomObjectList.Add(new RandomObject() {
            Name = "name 2"
        });
        randomObjectList.Add(new RandomObject() {
            Name = "name 3"
        });

        BindPivot();
    }

    private void BindPivot() {
        pivot.ItemsSource = new[] {
            new {
                TitleText = "pivot 1",
                List = randomObjectList
            },
            new {
                TitleText = "pivot 2",
                List = randomObjectList.Take(2).ToList()
            },
            new {
                TitleText = "pivot 3",
                List = randomObjectList.Take(1).ToList()
            }
        };
    }
}

I also have one class just to fill random data into the list box:

public class RandomObject {
  public string Name { get; set; }
}

Running this on the emulator gives the expected results as shown here:

pivot 1 screen shot pivot 2 screen shot pivot 3 screen shot

However, when I run this on the device there is simply nothing shown! Its an empty page, the only thing that shows is 'PIVOT TEST' at the top which is the title of the pivot control but no pivot items and ofc no list boxes.

The code above does not need any additions, you can make a test project and copy/paste the code above to check.

What could be the cause of this?

Thanks in advance!

edit: Forgot to mention that this is a Windows Phone OS 7.1 project. I don't know if that matters.

È stato utile?

Soluzione

After trying a few things I found out that the use of anonymous types is the cause of the page not displaying on the device, I am not sure why though. When I declare my typed classes and using those instead of anonymous types the page displays fine on the device.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top