在 Visual Studio 2008 Designer 中工作的 WPF 皮肤库

发布于 2024-09-17 19:54:57 字数 526 浏览 5 评论 0原文

在我们的项目中,我们的视图和控件位于许多不同的程序集中(单个解决方案中有 40 个程序集以上,具有 100 个视图)。我们希望将皮肤应用于单个皮肤组件中的所有视图,我们可以将其与不同的皮肤组件交换以应用不同的皮肤(不一定在运行时,这可以在编译时完成)并且只需要更改几行代码。更重要的是...我们希望它能够在 Visual Studio 2008 Designer 中正确显示。

目前,我们有一个运行时皮肤解决方案,通过将皮肤的主资源字典合并到 Application.Resources 中来应用适当的皮​​肤,但 Visual Studio 设计器不会在依赖项程序集中的视图上显示该皮肤。

我们可以将皮肤的资源字典合并到每个单独视图的 Window.Resources 中,但这会影响性能,并且如果我们想要交换皮肤,则需要进行许多更改。

我研究了在主题级别应用样式,但要在主题级别使用样式,您需要指定一个 ComponentResourceKey,其中包含对主题程序集中类型的强类型引用。这要求每个视图都指定皮肤所在的命名空间,这又是更换皮肤的主要重构。

还有其他想法吗?

In our projects, we have views and controls that are in many different assemblies (upwards of 40 assemblies in a single solution with 100s of views). We would like to apply a skin to all views from a single skin assembly that we can swap out with a different skin assembly to apply a different skin (not necessarily at runtime, this could be done at compile time) and only have to change a few lines of code. And here's the kicker... we want it to display properly in the Visual Studio 2008 Designer.

We currently have a runtime solution to skinning that applies the proper skin by merging the main resource dictionary of the skin into the Application.Resources, but the visual studio designer does not show that skin on the views in the dependency assemblies.

We could merge the skin's resource dictionary into each individual view's Window.Resources, but this is a performance hit and requires many changes if we want to swap out the skin.

I've looked at applying styles at the theme level, but to use a style at the theme level you need to specify a ComponentResourceKey with a strongly typed reference to a type in the theme assembly. That requires every single view to specify the namespace where the skin resides and again would be a major refactor to swap out skins.

Other ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吐个泡泡 2024-09-24 19:54:57

您可以使用附加行为将资源字典合并到每个窗口/页面/用户控件的资源,例如:

<Page my:Skinlibrary.Attach="True">
   <!-- stuff goes here -->
</Page>

您可以配置附加行为以始终将相同的资源字典实例合并到附加元素的资源。为此,您只需初始化资源字典一次,可能是在静态构造函数中。

假设您已在名为“_staticResourceDictionaryThatHoldsMySkin”的变量中准备好资源,则在附加属性的“PropertyChanged”回调中,您可以执行以下操作将其合并到附加窗口:

static void OnAttachChanged(DependencyProperty sender, DependencyPropertyChangedEventArgs e)
{
   (sender as FrameworkElement).Resources.MergedDictionaries.Add(_staticResourceDictionaryThatHoldsMySkinResources);
}

这将使您能够在编译时更改外观通过更改 SkinLibrary 文件。您还可以通过首先检查配置文件来加载资源字典。

根据我的经验,皮肤将在 Visual Studio 设计器(2008 和 2010)中正确显示。

You can use an attached behavior to merge your resource dictionary to every window/page/usercontrol's resource, like:

<Page my:Skinlibrary.Attach="True">
   <!-- stuff goes here -->
</Page>

You can configure the attached behavior to always merge the same resource dictionary instance to attached element's resources. To do this, you need to initialize the resource dictionary only once, maybe in a static ctor.

Assuming that you have the resources ready in a variable called "_staticResourceDictionaryThatHoldsMySkin", then in the "PropertyChanged" callback of the attached property, you can do this to merge it to attached windows:

static void OnAttachChanged(DependencyProperty sender, DependencyPropertyChangedEventArgs e)
{
   (sender as FrameworkElement).Resources.MergedDictionaries.Add(_staticResourceDictionaryThatHoldsMySkinResources);
}

This will enable you to change the skin during compile time just by changing the SkinLibrary file. You can also load the resource dictionary by inspecting a config file first.

In my experience, the skin will be displayed properly in Visual Studio designers, both 2008 and 2010.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文