Windows Phone 8 全景选择已更改 &数据绑定 [英] Windows Phone 8 Panorama SelectionChanged & Databinding

查看:26
本文介绍了Windows Phone 8 全景选择已更改 &数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote an app for Windows Phone 7, recently I've upgraded it to Windows Phone 8 and I plan on adding some features. Unfortunately, I've run into a problem immediately after the upgrade. The main part of the app is a Panorama control that is databound. On SelectionChanged I am fetching the data for the new PanoramaItem + 1 (preselecting data so it's there when the person eventually goes to the item). That worked fine in WP7 but the SelectionChanged event doesn't fire with WP8.

我用一个未升级的新 WP8 应用重现了这个问题,它也与数据绑定控件隔离.如果我静态添加 PanoramaItems,则 SelectionChanged 事件会很好地触发.

I've reproduced the issue with a new WP8 app that wasn't upgraded and it's also isolated to databound controls. If I statically add PanoramaItems the SelectionChanged event fires fine.

我是否遗漏了什么,或者这只是 WP8 中的一个直接错误?有什么推荐的解决方法吗?

Am I missing something or is this just a straight up bug in WP8? Any recommended work-arounds?

我有一个 GitHub 存储库,其中包含一个静态示例和一个数据绑定示例,以显示哪些有效,哪些无效.https://github.com/bthubbard/DatabindingIssues

I have a GitHub repo with a static sample and a databound sample to show what works and what doesn't work. https://github.com/bthubbard/DatabindingIssues

推荐答案

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

The Panorama control in WP8 has a known databinding bug. The symptoms of the bug are that SelectionChanged doesn't fire, SelectedIndex & SelectedItem aren't reliable and that back navigation into a page with Panorama resets the panorama selected item.

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

For example, the following code sample will never fire the MessageBox and SelectedIndex & SelectedItem won't indicate the correct expected values.

<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>

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

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

One obvious fix will be to manually initialize PanoramaItems in code-behind.

另一种解决方案是将我们的集合从类型化更改为非类型化,并将以下代码片段添加到我们的有界数据类中.因此,让我们将代码从 ObservableCollection 更改为 ObservableCollection 并向 Cow 类添加一些代码:

Another solution would be to change our collection from typed to untyped, and add the following code snippet to our bounded data class. So let's change our code from ObservableCollection<Cow> to ObservableCollection<object> and add some code to the Cow class:

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

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

Now, when we run this code snippet we can see SelectionChanged fires as expected with the correct SelectedIndex values:

这篇关于Windows Phone 8 全景选择已更改 &amp;数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆