默认样式在运行时无法从外部程序集解析
我在解析外部程序集中的资源时遇到了一些麻烦。
我在名为 Theme 的项目中有一个 Styles.xaml 文件,并且有一个默认按钮样式,其声明如下:
<Style TargetType="{x:Type Button}" x:Key="{x:Type Button}">
<!--Setters here-->
</Style>
然后在一个单独的 WPF 项目(但在同一解决方案中)中,我在 app.xaml 文件中有以下内容:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Theme;component/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
然后在主窗口中,我声明一个没有附加样式的默认按钮,如下所示:
<Button Width="100" Height="100" />
在设计视图中,按钮似乎从 Styles.xaml 文件中获取样式,但是当我构建并运行项目时,按钮默认为标准按钮样式。
我已经检查过 Theme.dll 文件已被复制到 WPF 构建目录(并且确实如此),所以我不知道为什么会发生这种情况。
有趣的是,如果我像这样定义按钮样式
<Style TargetType="{x:Type Button}" x:Key="MyStyle">
,然后像这样直接在其他项目中的按钮上引用它,
<Button Style={StaticResource MyStyle} Width="100" Height="100" />
它会在设计视图中选择样式,并在项目构建和执行时正常工作。
有什么想法吗?任何帮助都会很棒!
克里斯
I’m having a bit of trouble with resolving resources from an external assembly.
I have a Styles.xaml file in a project called Theme and I have a Default Button style which has been declared as follows:
<Style TargetType="{x:Type Button}" x:Key="{x:Type Button}">
<!--Setters here-->
</Style>
And then in a separate WPF project (but in the same solution) I have the following in the app.xaml file:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Theme;component/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Then in the main Window, I declare a default Button with no style attached to it like so:
<Button Width="100" Height="100" />
In design view, the button appears to pick up the style from the Styles.xaml file, but when I build and run the project, the Button just defaults to the standard button style.
I have checked to see that the Theme.dll file has been copied across to the WPF build directory (and it has) so I don’t know why this is happening.
Interestingly, if I define the Button Style like this
<Style TargetType="{x:Type Button}" x:Key="MyStyle">
And then reference it directly on the Button in the other project like this
<Button Style={StaticResource MyStyle} Width="100" Height="100" />
It picks up the style in design view and works normally when the project is built and executed.
Any ideas? Any help would be great!
Kris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要在引用 XAML 文件的位置使用完整的 pack URI ,即
siteoforigin
(如果您不引用嵌入资源)。You may need to use a complete pack URI where you reference the XAML file, namely with
siteoforigin
if you don't reference embedded resources.