资源字典添加x:Class

4

在第一次尝试创建DataTemplate时,我将MainWindow的类添加到其中以访问事件处理程序。现在,这可能有许多原因是不正确的(并且会产生一些有趣的错误消息),但我想了解为什么ResourceDictionary无法引用像MainWindow这样的部分类?

xaml如下(请注意,如果没有实现任何事件,则会失败)

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Window.Resources>
    <ResourceDictionary>
            <ResourceDictionary Source="Dictionary1.xaml"/>
    </ResourceDictionary>
</Window.Resources>
</Window>

资源字典。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MainWindow"
>

</ResourceDictionary>

非常感谢您的帮助和建议。

1个回答

4

您不能使用类似于MainWindow的东西作为资源字典的后备,因为MainWindow并未继承ResourceDictionary类。

请查看此文章以更好地理解代码背后资源字典的使用...

希望这回答了您的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接