Windows Phone 8全景图选择已更改&数据绑定 [英] Windows Phone 8 Panorama SelectionChanged & Databinding
问题描述
我为Windows Phone 7编写了一个应用程序,最近将其升级到Windows Phone 8,并计划添加一些功能.不幸的是,升级后我立即遇到了问题.该应用程序的主要部分是数据绑定的Panorama控件.在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中的Panorama控件具有一个已知的数据绑定错误.该错误的症状是SelectionChanged不触发,SelectedIndex& SelectedItem不可靠,并且向后导航到带有Panorama的页面会重置全景图所选的项目.
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<Cow>
更改为ObservableCollection<object>
并将一些代码添加到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();
}
}
现在,当我们运行此代码片段时,我们可以看到使用正确的SelectedIndex值按预期方式触发了SelectionChanged:
Now, when we run this code snippet we can see SelectionChanged fires as expected with the correct SelectedIndex values:
这篇关于Windows Phone 8全景图选择已更改&数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!