本文介绍了WPF 基础:MVVM 的共享全局样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MVVM 风格的方法进行 WPF 开发.

I am attempting to use an MVVM-ish approach to my WPF development.

我在 ViewModel 命名空间下有我的逻辑视图模型类,并且我在 View 命名空间下有这些视图模型类的匹配样式.

I have my logical view model classes under the ViewModel namespace, and I have matching styling for these view model classes under the View namespace.

现在,我在 ResourceDictionary XAML 文件中有我的视图信息,作为数据模板和样式,它们都合并到 app.xaml 中的单个 App.Resources ResourceDictionary 中.

For now I have my View information in ResourceDictionary XAML files, as DataTemplates and Styles, which are all merged into the single App.Resources ResourceDictionary in app.xaml.

但是,我遇到了一种鸡/蛋问题.我希望有我在所有地方使用的全局样式.例如,我想要我自己的名为 MonkeyText 的自定义文本样式,它可以在所有地方用于各种样式.我不能只在 app.xaml 文件中设置它,因为要使用 MonkeyText 的资源字典包含在该 app.xaml 文件中.

However, I'm running into a sort of chicken/egg problem. I want there to be global styles that I use all over the place. For example, I want my own custom text style called MonkeyText which could be used in various stylings all over the place. I can't just set this in the app.xaml file, because the resourcedictionarys that will want to use MonkeyText are included by that app.xaml file.

我想如果这是不可能的,另一种方法是使用 UserControls 而不是主要使用 DataTemplates 来建立我的视图?我担心使用 UserControls 会将 VM 和 V 部分联系得太紧密.

I guess if that's impossible an alternative would be to use UserControls instead of mostly using DataTemplates to establish my views? I'm afraid that using UserControls would tie the VM and V parts too closely together.

推荐答案

WPF 正是出于这个原因提供了 DynamicResources.静态资源 - 最类似于编程中的传统"引用 - 只是您遇到的问题;它们需要在解析样式之前定义和加载.另一方面,DynamicResources 不需要在使用它们之前定义——实际上,您甚至可以即时创建它们.WPF 负责确保 DynamicResources 在实际加载后由所有引用它们的样式自动加载.

WPF provides DynamicResources for just this reason. StaticResources - which most resemble 'traditional' references in programming - have just the problem you encountered; they need to be defined and loaded prior to the point that a style is parsed. DynamicResources, on the other hand, do not need to be defined prior to the point they are used - indeed, you can even create them on the fly. WPF takes care of ensuring that DynamicResources are automatically loaded by all of the styles that reference them once they are actually loaded.

使用 DynamicResources 很简单.创建 MonkeyText 样式时,请像往常一样创建它:

Using DynamicResources is straightforward. When you create your MonkeyText style, create it as you normally would:

<Style TargetType="TextBlock" x:Key="MonkeyText">
    <Setter Property="TextAlignment" Value="Center"/>
    <!-- etc. -->
</Style>

然后使用 DynamicResource 从别处引用它:

And then refer to it from elsewhere using a DynamicResource:

<TextBlock Text="Hello, World!" Style="{DynamicResource MonkeyText}"/>

如果 WPF 由于任何原因无法解析您的 DynamicResource,它将静默失败,不会抛出任何异常(当无法解析时,StaticResources 确实会抛出异常).但是,它会在发生这种情况时打印一条调试消息 - 因此请注意 Visual Studio 中的输出"窗口.

If, for any reason, WPF cannot resolve your DynamicResource, it will fail silently without any exception thrown (StaticResources do throw exceptions when the cannot be resolved). It will, however, print a debug message when this happens - so keep an eye on the Output window in Visual Studio.

由于 DynamicResources 处理以任何顺序在任何时间点加载的资源,您可以按照您喜欢的任何方式构建您的资源字典 - 因此将它们与您的其他视图样式一起使用并通过单个 App.Resources ResourceDictionary 将它们合并app.xaml 可以正常工作.

Since DynamicResources work with resources that are loaded at any point in any order, you can structure your resource dictionaries any way you like - so putting them in with your other View styles and merging them in via the single App.Resources ResourceDictionary in app.xaml will work fine.

可以在 WPF 的 MSDN 文档中找到有关 DynamicResources 的更多详细信息.

More details on DynamicResources can be found in the MSDN docs for WPF.

这篇关于WPF 基础:MVVM 的共享全局样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:22