Windows Phone 8 Panorama SelectionChanged&Databinding

Bra*_*don 9 silverlight windows-phone-7 panorama-control windows-phone-8

我为Windows Phone 7编写了一个应用程序,最近我将其升级到Windows Phone 8,我计划添加一些功能.不幸的是,我在升级后立即遇到了问题.该应用程序的主要部分是数据绑定的全景控件.在SelectionChanged上,我正在获取新的PanoramaItem + 1的数据(预选数据,以便当人最终进入该项时它就在那里).这在WP7中运行良好,但SelectionChanged事件不会触发WP8.

我已经使用未升级的新WP8应用程序重现了这个问题,并且它也与数据绑定控件隔离.如果我静态添加PanoramaItems,SelectionChanged事件会触发.

我错过了什么,或者这只是WP8中的一个直接错误?任何推荐的解决方案?

我有一个带有静态样本的GitHub仓库和一个数据绑定样本,以显示哪些有效,哪些无效. https://github.com/bthubbard/DatabindingIssues

Jus*_*gel 18

WP8中的Panorama控件有一个已知的数据绑定错误.该错误的症状是SelectionChanged不会触发,SelectedIndex和SelectedItem不可靠,并且返回导航到带有Panorama的页面会重置全景选定项目.

例如,以下代码示例永远不会触发MessageBox,SelectedIndex和SelectedItem将不会指示正确的期望值.

<phone:Panorama x:Name="panorama"
                ItemsSource="{Binding}" 
                SelectionChanged="Panorama_SelectionChanged_1">
    <phone:Panorama.HeaderTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding Name}" />
        </DataTemplate>
    </phone:Panorama.HeaderTemplate>
    <phone:Panorama.ItemTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding Name}" />
        </DataTemplate>
    </phone:Panorama.ItemTemplate>
</phone:Panorama>
Run Code Online (Sandbox Code Playgroud)
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    this.DataContext = new ObservableCollection<Cow>()
                           {
                               new Cow("Foo"),
                               new Cow("Bar"),
                               new Cow("Baz")
                           };
}

private void Panorama_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
    MessageBox.Show("Panorama_SelectionChanged_1: " + panorama.SelectedIndex);
}

public class Cow
{
    public Cow(string name)
    {
        Name = name;
    }

    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

一个明显的解决方法是在代码隐藏中手动初始化PanoramaItems.

另一个解决方案是将我们的集合从typed更改为untyped,并将以下代码片段添加到我们的有界数据类中.因此,让我们的代码更改ObservableCollection<Cow>ObservableCollection<object>一些代码添加到Cow类:

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    this.DataContext = new ObservableCollection<object>()
                           {
                               new Cow("Foo"),
                               new Cow("Bar"),
                               new Cow("Baz")
                           };
}

public class Cow
{
    public Cow(string name)
    {
        Name = name;
    }

    public string Name { get; set; }

    public override bool Equals(object obj)
    {
        if ((obj != null) && (obj.GetType() == typeof(PanoramaItem)))
        {
            var thePanoItem = (PanoramaItem)obj;

            return base.Equals(thePanoItem.Header);
        }
        else
        {
            return base.Equals(obj);
        }
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我们运行此代码片段时,我们可以看到SelectionChanged按预期使用正确的SelectedIndex值触发:

全景使用正确的SelectedIndex触发SelecitonChanged事件 全景使用正确的SelectedIndex触发SelecitonChanged事件

  • 感谢您的答复.你的解决方案在一个小应用程序上运行得很好,但可能会有一些更多涉及更多的痛苦.知道何时修复? (3认同)