【发布时间】:2012-12-06 14:26:49
【问题描述】:
我有一个这样定义的类(我删除了一些代码行,例如空检查,以保持简短):
public sealed class CodeTheme : INotifyPropertyChanged
{
public void Reload()
{
PropertyChanged(sender, new PropertyChangedEventArgs("MyProperty"));
}
public Thickness MyProperty
{
get
{
return new Thickness():
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
我正在使用 StandardStyles.xaml 像这样在 XAML 中注册代码主题类
<me:CodeTheme x:Key="Theming" />
然后,我在各种模板和样式中使用它,如下所示:
<Style x:Key="Style1" TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="Margin" Value="{Binding Source={StaticResource Theming}, Path=MyProperty}" />
</Style>
然后我可能会在程序中的任何时候调用Reload方法,第一次在App.xaml.cs的覆盖OnLaunched事件中
当我在DataTemplate 中分配颜色、字符串和其他厚度值时,这适用于它们。但是,当我在Style 中使用它时,一旦触发PropertyChanged 事件,就会出现以下错误:
System.UnauthorizedAccessException 未被用户代码处理 H结果=-2147024891 消息=访问被拒绝。 (来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED)) 源=系统 StackTrace:(我已经删除了这个) 内部异常:
我试图通过RunAsync 从Window.Current.Dispatcher 提出事件,但这并没有改变任何事情。我在这里做错了什么?
【问题讨论】:
-
你确定 PropertyChanged 不为空吗?
-
@ArsenMkrt 是的,我完全确定,因为原始来源中有一个空检查(正如我在第一句话中提到的,我删除了一些行以保持简短)
-
多一点堆栈跟踪可能会有所帮助。
标签: c# xaml windows-runtime winrt-xaml windows-store-apps