在处理托管自定义 Windows 8 控件时,我遇到了“灾难性故障”异常,我已经设法将问题本地化为一个非常简单的测试用例。现在我被困住了。
假设我有一个这样定义的枚举:
public enum Modes
{
Mode1,
Mode2
}
然后我有一个自定义控件,其依赖属性定义如下
public Modes Mode
{
get { return (Modes)GetValue(ModeProperty); }
set { SetValue(ModeProperty, value); }
}
// Using a DependencyProperty as the backing store for Mode. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ModeProperty =
DependencyProperty.Register("Mode", typeof(Modes), typeof(CustomControl1), new PropertyMetadata(Modes.Mode1));
我尝试通过 VisualState 将属性从 Mode1 切换到 Mode2,如下所示:
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStateGroup">
<VisualState x:Name="VisualState">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(CustomControl1.Mode)" Storyboard.TargetName="customControl1">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<local:Modes>Mode2</local:Modes>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
为此,我只需在单击按钮时调用 GoToState() :
private void Button_Click_1(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this, "VisualState", false);
}
我得到了臭名昭著的“灾难性故障(HRESULT 异常:0x8000FFFF(E_UNEXPECTED))”
我试图在 Silverlight 中创建完全相同的测试用例,它工作得很好。这是 Windows 8 XAML RC 错误还是我做错了什么?