Silverlight PRISM 和“松散”样式文件

发布于 2024-08-05 10:29:35 字数 229 浏览 1 评论 0原文

我们正在考虑使用 PRISM 框架启动一个新的 Silverlight 项目(以从模块等中受益),但我仍然不太清楚最佳的样式方法。我理想的情况是拥有包含应用程序样式的可编辑 XAML 文件(整个项目甚至可能只有 1 个),以便可以编辑它们来更改应用程序的外观,而无需重新编译所有内容。 人们使用这种方法吗?我想它需要在启动时加载文件并应用我认为不会是巨大开销的样式。

只是想知道人们使用什么方法

感谢您的宝贵时间

We are looking at starting a new Silverlight project using the PRISM framework (to benefit from modules etc) and I am still a little unclear about the best styling approach. What I'd ideally like is to have editable XAML files (maybe even just 1 for the whole project) containing the application's style so that they can be edited to change the look and feel of the application without having to recompile everything.
Is this approach something people use? I guess it would need to load the file in at startup and apply the style which I assume wouldn't be a massive overhead.

Just wondering what approaches people use

Thanks for your time

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

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

发布评论

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

评论(2

淡忘如思 2024-08-12 10:29:35

在 Silverlight(和 WPF)中,典型/正常的方法是将样式和画笔保存在资源字典中。然后可以随时更换它们以更改布局、颜色和控制模板。您可以将它们包含在构建应用程序时生成的 XAP 中或独立程序集中。

需要记住的是,资源文件可以在不同的级别加载。应用程序级别深入到各个控件的级别。在较低级别加载的词典将优先于较高级别的词典。对我来说,把它想象成层次是有帮助的。

In Silverlight (and WPF) the typical/normal approach is to keep your styles and brushes in a Resource Dictionary. these can then be swapped out to change the layout, colors, and control templates at any time. You can include them in the XAP that is generated when you build the app or in a stand-alone assembly.

The thing to remember is that the resource files can be loaded at differing levels. The application level down to the level of the individual controls. the dictionaries that are loaded at the lower level will take precedence over the higher level one. For me, it helps to think of it like layers.

鹤舞 2024-08-12 10:29:35

这是我的方法 -

在您的 App.xaml 中,您需要声明一个像这样的 MergedDictionaries 元素。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles\Colors.xaml" />
            <ResourceDictionary Source="Styles\Brushes.xaml" />
            <ResourceDictionary Source="Styles\Typeography.xaml" />
            <ResourceDictionary Source="Styles\ModuleAStyles.xaml />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

如您所见,我倾向于在这里有一些单独的颜色文件(如果您'正确拼写)、画笔、排版,然后我通常为每个模块额外使用一个。如果它是一个大型应用程序,请使用多个文件,否则您最终会在一个文件中包含太多内容,并且很快就会变得难以维护。您只需在这里运用您的最佳判断,看看什么最适合您。

在我在该合并字典中引用的 Typeography.xaml 中,我声明了一个名为 ApplicationTitle 的样式,如下所示...

<!--Application Title-->
<Style TargetType="{x:Type TextBlock}"
       x:Key="ApplicationTitle">
    <Setter Property="VerticalAlignment"
            Value="Center" />
    <Setter Property="HorizontalAlignment"
            Value="Center" />
    <Setter Property="FontFamily"
            Value="Georgia" />
    <Setter Property="Foreground"
            Value="{StaticResource ResourceKey=FlatGradientLightest}" />
    <Setter Property="FontSize"
            Value="24" />
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect BlurRadius="5"
                              Color="#333"
                              Opacity=".3" />
        </Setter.Value>
    </Setter>
</Style>

您会注意到,在这种样式中,我再次使用 引用另一个名为“FlatGradientLightest”的资源StaticResource 标记扩展。该资源存在于 Brushes.xaml 文件中...

    <!--Flat Diagonal Gradient Lightest-->
<LinearGradientBrush x:Key="FlatDiagonalGradientLightest"
                     StartPoint="0,0"
                     EndPoint="1,1">
    <GradientStop Color="{StaticResource ResourceKey=Light}"
                  Offset="0" />
    <GradientStop Color="{StaticResource ResourceKey=Lightest}"
                  Offset="1" />
</LinearGradientBrush>

并且再次引用颜色“Light”和“Lightest”。这些存在于 Colors.xaml 文件中...

<Color x:Key="Light"
       A="255"
       R="190"
       G="190"
       B="190" />
<Color x:Key="Lightest"
       A="255"
       R="250"
       G="250"
       B="250" />

这里重要的是我在 App.xaml 中指定资源字典的顺序。如果我将 Typeography.xaml 移至列表顶部,则会导致运行时,因为在加载此样式时,它的依赖项将不存在。

由于 WPF/SL 向上解析资源(如 Muad'Dib)指出的,因此您可以在模块中使用这些资源,就像它们是在本地声明的一样。因此,在我的一个模块中,我有一个像这样声明的 TextBlock...

<TextBlock Text="Menu Module Loaded" Style="{StaticResource ResourceKey=ApplicationTitle}" />

TextBlock 现在使用 Typeography.xaml 中的“ApplicationTitle”样式,该样式加载它的 Foreground 笔刷来自 Brushes.xaml 中的“FlatGradientLightest”LinearGradientBrush,它又从 Colours.xaml 文件中的 Color 资源加载两种颜色。

有点酷吧?

希望有帮助。

Here's my approach-

In your App.xaml you'll want to declare a MergedDictionaries element like this..

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles\Colors.xaml" />
            <ResourceDictionary Source="Styles\Brushes.xaml" />
            <ResourceDictionary Source="Styles\Typeography.xaml" />
            <ResourceDictionary Source="Styles\ModuleAStyles.xaml />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

As you can see, I tend to have a few seperate files here for colors (Colours if you're spelling it properly), brushes, Typeography and then I generally use one extra for each Module. Use more than one if it's a large app else you'll end up with too much stuff in one file and it quickly gets difficult to maintain. You'll just have to use your best judgement here and see what fits best for you.

In the Typeography.xaml that I'm referencing in that merged dictionary I have declared a style called ApplicationTitle like this...

<!--Application Title-->
<Style TargetType="{x:Type TextBlock}"
       x:Key="ApplicationTitle">
    <Setter Property="VerticalAlignment"
            Value="Center" />
    <Setter Property="HorizontalAlignment"
            Value="Center" />
    <Setter Property="FontFamily"
            Value="Georgia" />
    <Setter Property="Foreground"
            Value="{StaticResource ResourceKey=FlatGradientLightest}" />
    <Setter Property="FontSize"
            Value="24" />
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect BlurRadius="5"
                              Color="#333"
                              Opacity=".3" />
        </Setter.Value>
    </Setter>
</Style>

You'll notice that in this style I'm again referencing another resource called 'FlatGradientLightest' using the StaticResource markup extension. This resource exists in the Brushes.xaml file...

    <!--Flat Diagonal Gradient Lightest-->
<LinearGradientBrush x:Key="FlatDiagonalGradientLightest"
                     StartPoint="0,0"
                     EndPoint="1,1">
    <GradientStop Color="{StaticResource ResourceKey=Light}"
                  Offset="0" />
    <GradientStop Color="{StaticResource ResourceKey=Lightest}"
                  Offset="1" />
</LinearGradientBrush>

And again this references the Colors 'Light' and 'Lightest'. These exist in the Colors.xaml file...

<Color x:Key="Light"
       A="255"
       R="190"
       G="190"
       B="190" />
<Color x:Key="Lightest"
       A="255"
       R="250"
       G="250"
       B="250" />

What's important here is the order I specified the resource dictionaries in App.xaml. If I were to move Typeography.xaml to the top of the list, it would cause a runtime since at the time it loaded this style, it's dependencies wouldn't exist.

Since WPF/SL look upwards to resolve resources (as Muad'Dib) pointed out, you can just use these resources in your modules as if they were declared locally. So in one of my modules, I have a TextBlock declared like this...

<TextBlock Text="Menu Module Loaded" Style="{StaticResource ResourceKey=ApplicationTitle}" />

That TextBlock now uses the 'ApplicationTitle' style in Typeography.xaml, which loads it's Foreground brush from the 'FlatGradientLightest' LinearGradientBrush in Brushes.xaml, which in turn loads two colors from Color resources in the Colours.xaml file.

Kinda cool right?

Hope that helps.

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