【发布时间】:2023-03-25 02:39:01
【问题描述】:
我有一个适用于 WP 8.1 的通用应用程序,其中包含一个页面和一个 Pivot。枢轴的“SelectedIndex”属性绑定到 VM 中的一个属性,如下所示:
public object SelectedPivotIndex
{
get { return this.selectedPivotIndex; }
set
{
if (this.selectedPivotIndex == value) return;
this.selectedPivotIndex = value;
RaisePropertyChanged(() => SelectedPivotIndex);
}
}
页面代码:
<Pivot x:Name="ContentPivot"
x:Uid="ContentPivot"
SelectedIndex="{Binding SelectedPivotIndex, Mode=TwoWay}"
>...</Pivot>
问题是我有时会遇到应用程序崩溃(在 App.xaml.cs 中):“未处理的异常”,类型为“COMEXCEPTION”。如果我删除 xaml 中“SelectedIndex”的绑定,则此崩溃停止,但我不明白它为什么会发生。有时甚至没有显示调试器,并且应用程序在没有任何错误信息的情况下关闭。 顺便说一句,我使用的是 MVVM Light,因此视图(页面)和 VM 之间的“胶水”设置在页面中:
<Page
...
DataContext="{Binding Source={StaticResource Locator}, Path=Main}"
>
编辑:
我能够通过这种行为重现崩溃:打开应用程序,导航到另一个页面,返回到数据透视页面(几次)并翻阅数据透视项。
【问题讨论】:
-
索引是一个int值,你为什么用object?
-
@Fred 那是我对该属性的初始类型,但它会导致强制转换异常,我不明白为什么......
-
如果没有堆栈跟踪,这很难说。然而,这对我来说看起来很可疑。
标签: windows-phone-8.1 mvvm-light