应用样式时出现异常

Ale*_*lex 4 .net wpf resources xaml styles

我敢肯定我正在努力学习一些非常基本的东西,错过了一个非常简单的观点...但我只是一直打墙,所以请帮忙.

问题如下.

如果我<Window.Resources>在同一个窗口中定义某个样式,我将其应用于控件 - 一切正常.但是因为我想在我的应用程序的不同窗口中重用这个样式,所以我试图将样式移到一个常见的位置......而且这里的东西停止了工作.

如果我将样式放在Themes\Generic.xaml中,则样式不会被应用.

当我尝试通过显式应用Style="{DynamicResource MyStyle}"相应的控件(其中"MyStyle"是样式的x:Key)从原始窗口引用它时- 我收到错误

无法解析资源"MyStyle"

如果我将该样式放在一个单独的XAML文件中,并尝试将其添加到App.xaml中的MergedDictionaries中,我会遇到另一个问题:"'Resources'属性已经在'App'上设置了".这是我尝试定义它的方式:

<Application.Resources>
    <ResourceDictionary x:Key="MergedDictionaries">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Themes/MyStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
<Application.Resources>
Run Code Online (Sandbox Code Playgroud)

H.B*_*.B. 6

你不应该x:Key在字典上设置,否则它将被添加为你想要的资源,它应该设置Application.Resources属性.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- Merged dictionaries -->
        </ResourceDictionary.MergedDictionaries>

        <!-- Other Resources -->
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)