【问题标题】:Pivot control with item template works on emulator but not on actual device带有项目模板的枢轴控制适用于模拟器,但不适用于实际设备
【发布时间】:2013-02-20 21:55:10
【问题描述】:

当我对模拟器进行测试时,我的页面显示正常。但是,当我在我的设备上运行它时,页面是空的!

我正在使用项目模板制作枢轴控件,在 Pivot.ItemTemplate 内我还有一个带有 ListBox.ItemTemplate 的 ListBox

下面的代码应生成一个标题为“PIVOT TEST”的页面,其中包含 3 个数据透视项:“数据透视 1”、“数据透视 2”、“数据透视 3”。在每个枢轴内,应该有一个列表。对于“pivot 1”,列表中应该有 3 个项目:“name 1”、“name 2”、“name 3”。对于“枢轴 2”,列表中应该有 2 个项目:“名称 1”、“名称 2”。对于“pivot 3”,列表中应该有 1 项:“name 1”

这里是 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>
...

这是整个页面的代码:

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()
            }
        };
    }
}

我也有一个类只是为了将随机数据填充到列表框中:

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

在模拟器上运行它会得到预期的结果,如下所示:

但是,当我在设备上运行它时,根本没有任何显示!它是一个空页面,唯一显示的是顶部的“PIVOT TEST”,它是枢轴控件的标题,但没有枢轴项,并且没有列表框。

上面的代码不需要任何添加,你可以做一个测试项目,复制/粘贴上面的代码来检查。

这可能是什么原因?

提前致谢!

编辑: 忘了说这是一个 Windows Phone OS 7.1 项目。我不知道这是否重要。

【问题讨论】:

    标签: windows-phone-7 windows-phone windows-phone-7-emulator


    【解决方案1】:

    在尝试了几件事后,我发现使用匿名类型是导致页面未在设备上显示的原因,但我不知道为什么。当我声明我的类型化类并使用它们而不是匿名类型时,页面在设备上显示正常。

    【讨论】:

    • @AlaaMasaoud 嗨,我在模拟器中遇到了同样的问题,我的绑定工作正常,但在设备上不工作。我想做什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2011-03-08
    • 2015-02-13
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多