在WPF类库大会宽/ root级别的样式 [英] Assembly-wide / root-level styles in WPF class library

查看:295
本文介绍了在WPF类库大会宽/ root级别的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#(2008 / .NET 3.5)类库组件(基于<一href="http://dotupdate.word$p$pss.com/2007/12/05/how-to-add-a-wpf-control-library-template-to-visual-c-ex$p$pss-2008/">http://dotupdate.word$p$pss.com/2007/12/05/how-to-add-a-wpf-control-library-template-to-visual-c-ex$p$pss-2008/).我创建了几个窗口,而我现在正在尝试建立一个共同的风格为他们设置的。但是,因为它是一个类库(而不是一个WPF应用程序),我没有一个App.xaml中(及其包含的应用与放大器;相应Application.Resources)来存储这些样式的全局访问

I have a C# (2008/.NET 3.5) class library assembly that supports WPF (based on http://dotupdate.wordpress.com/2007/12/05/how-to-add-a-wpf-control-library-template-to-visual-c-express-2008/). I've created several windows, and am now attempting to create a common style set for them. However, as it's a class library (instead of a WPF app), I don't have an app.xaml (and its contained Application & corresponding Application.Resources) in which to store these styles for global access.

所以:如何创建一个顶级一套将所有XAML文件在装配中可以看出样式定义,因为我没有App.xaml中(见上文)的?和/或可能的添加的工作App.xaml中的一类库?

So: How can I create a top-level set of style definitions that'll be seen by all xaml files in the assembly, given that I do not have app.xaml (see above)? And/or is it possible to add a working app.xaml to a class library?

仅供参考,我曾尝试创建一个ResourceDictionary.xaml文件的ResourceDictionary,并将其包含在每个窗口Window.Resources块内。这横空出世解决了按钮等的造型......而不是封闭的窗口。我可以把'风格={的StaticResource MyWindowStyle}'在窗口的开放块,它编译并显示在VS设计窗口罚款,但在实际运行时,我收到了分析异常(MyWindowStyle找不到,我'猜的Visual Studio看到包括有问题的行之后的字典,但CRL做事顺序多,因此还没有加载的ResourceDictionary至今)。

FYI, I did try creating a ResourceDictionary in a ResourceDictionary.xaml file, and include it in each window within a "Window.Resources" block. That turned out to solve the styling of Buttons, etc... but not for the enclosing Window. I can put 'Style="{StaticResource MyWindowStyle}"' in the Window's opening block, and it compiles and shows up in the VS Design window fine, but during actual runtime I get a parse exception (MyWindowStyle could not be found; I'm guessing Visual Studio sees the dictionary included after the line in question, but the CRL does things more sequentially and therefore hasn't loaded the ResourceDictionary yet).

谢谢你的想法,但仍然没有去...显然是一个类库不支持generic.xaml使用隐式。我说generic.xaml我的类库项目,并设置其生成操作为资源。它包含:

Thanks for the ideas, but still no go... apparently a class library does NOT support the generic.xaml usage implicitly. I added generic.xaml to my class library project and set its Build Action to "Resource". It contains:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type Window}" x:Key="MyWindow">
        <Setter Property="Background" Value="Black"/>
    </Style>
</ResourceDictionary>

这是我想要使用的窗口XAML的主题是这样的:

The window xaml that I want to have use the theme looks like this:

<Window x:Class="MyAssembly.ConfigureGenericButtons"
    x:ClassModifier="internal"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Style="{StaticResource MyWindow}"
    Title="ConfigureGenericButtons">
...Buttons, etc...
</Window>

尽管VS设计窗口不显示使用mywindow的样式(即黑色背景)的窗口,它编译罚款并启动。然而,当含有此类库的应用程序,使得调用导致此窗口中显示,我得到一个XamlParseException:

Although the VS Design window doesn't show the window using the MyWindow style (ie black background), it compiles fine and starts up. However, when the app containing this class library makes a call that causes this window to display, I get a XamlParseException:

Cannot find resource named '{MyWindow}'.

我也试过离开了样式参数,看看是否窗口将使用样式默认情况下(和我想这两个与X:关键在generic.xaml包括,无)。没有错误,但任何在generic.xaml定义没有出现任何。

I also tried leaving out the Style parameter, to see if the window would use the style by default (and I tried that both with the x:Key in generic.xaml included and without). No errors, but anything defined in the generic.xaml didn't show up either.

难道我在这里做得不对,或使用一个窗口人们如何可能允许普通自定义样式任何其他想法(即,不必在每个窗口的XAML定义样式) - 需要提醒的是这个是不是一个应用程序?

Am I doing something wrong here, or any other ideas on how one might allow for common custom styles to be used on a Window (ie, not have to define the styles in each Window's xaml) -- with the caveat that this is NOT an application?

推荐答案

尝试添加

Style={DynamicResource MyStyle}

您不能在这种情况下使用的StaticResource。

You cannot use a StaticResource in this case.

这篇关于在WPF类库大会宽/ root级别的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆