WPF 应用程序复杂皮肤

发布于 2024-07-18 08:36:32 字数 703 浏览 6 评论 0原文

我正在尝试找出如何使用复杂资源来为应用程序换肤。

我有一个皮肤文件,其中放置了包含复杂艺术品的 Canvas 。 像这样:

<ResourceDictionary>
    <Style x:Key="MainBackground" TargetType="{x:Type Canvas}">
        <Setter Property="Canvas">
            <Setter.Value>
                <Canvas Width="1440.000" Height="900.000">
                <!-- complicated artwork here -->
                </Canvas>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

如何将此皮肤加载到主应用程序中? 我在想这样的事情:

<Window>
    <Canvas Style="{StaticResource MainBackground}"/>
</Window >

I'm trying to figure out how to skin an application with a complex resource.

I have a skin file in which I put Canvas that contains the complicated artwork. Like this:

<ResourceDictionary>
    <Style x:Key="MainBackground" TargetType="{x:Type Canvas}">
        <Setter Property="Canvas">
            <Setter.Value>
                <Canvas Width="1440.000" Height="900.000">
                <!-- complicated artwork here -->
                </Canvas>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

How can I load this skin into the main app? I'm thinking something like this:

<Window>
    <Canvas Style="{StaticResource MainBackground}"/>
</Window >

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

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

发布评论

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

评论(3

困倦 2024-07-25 08:36:32

如果我正确理解了问题:您可以从导出的 XAML 文件的根视觉对象创建视觉画笔,并将其用作另一个画布的画笔。

If I understood the question correctly: You can create a visual brush from the exported XAML file's root visual, and use it as the brush of another canvas.

不疑不惑不回忆 2024-07-25 08:36:32

我知道这是一个老问题......但有几种方法可以解决这个问题。 为了希望这对谷歌搜索者有所帮助,我将分享两种方法。

首先,您可以简单地在该 Canvas 周围放置一个 Viewbox(从转换后的 .ai 文件中获得)。 请注意,您可能必须调整“拉伸”和“对齐”属性才能使其按您希望的方式运行...但很可能您会将“拉伸”设置为“UniformToFill”,并将对齐属性设置为“居中”:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="BackgroundSkin.MainWindow"
    x:Name="Window"
    Width="640"
    Height="480"
>
    <Grid x:Name="LayoutRoot">
        <Viewbox
            Stretch="UniformToFill"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
        >
            <!-- Replace the following Canvas with yours. -->
            <Canvas Background="White"/>
        </Viewbox>
    </Grid>
</Window>

其次,如果您宁愿有一些更可共享的东西(这样您可以将其用作多个 Windows 上的背景),您可以从该图稿中创建一个画笔。 有几种方法可以做到这一点。 第一个是简单地认识到您可以使用 Expression Design 将 .ai 文件转换为 DrawingBrush(即 Expression Design 可以通过两种主要方式导出:Canvas/Shape(s)或 ResourceDictionary/Brush(es))。

如果您没有用于重新导出的原始 .ai/.design 文件,您可以在 Canvas 上创建 VisualBrush(如 @Ugar Turan 建议的那样)。 请注意,您可能再次需要在 VisualBrush 上使用 Stretch 属性(和其他属性):

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="BackgroundSkin.MainWindow"
    x:Name="Window"
    Width="640"
    Height="480"
    Background="{DynamicResource backgroundBrush}"    
>
    <Window.Resources>
        <!-- Replace the following Canvas with yours. -->
        <Canvas x:Key="backgroundCanvas"/>
        <VisualBrush
            x:Key="backgroundBrush"
            Visual="{DynamicResource backgroundCanvas}"
            Stretch="UniformToFill"
        />
    </Window.Resources>
</Window>

I know this is an old question ... but there are several way to go about this. In the hope that this will help a Google searcher, I will share two ways.

First of all, you could simply put a Viewbox around that Canvas (that you got from a converted .ai file). Be aware that you may have to monkey with the Stretch and alignment properties in order to get it to behave as you wish ... but most likely you would set Stretch to UniformToFill and the alignment properties to Center:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="BackgroundSkin.MainWindow"
    x:Name="Window"
    Width="640"
    Height="480"
>
    <Grid x:Name="LayoutRoot">
        <Viewbox
            Stretch="UniformToFill"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
        >
            <!-- Replace the following Canvas with yours. -->
            <Canvas Background="White"/>
        </Viewbox>
    </Grid>
</Window>

Secondly, if you would rather have something more shareable (such that you could use it as the background on several Windows), you could create a Brush out of that artwork. There are a couple ways to do this. The first is simply to realize that you can use Expression Design to convert the .ai file into a DrawingBrush (i.e. Expression Design can export in two main ways: Canvas/Shape(s) or ResourceDictionary/Brush(es)).

If you don't have the original .ai/.design file for re-exporting, you can create a VisualBrush off of the Canvas (as @Ugar Turan suggests). Note that you will again likely have to monkey around with the Stretch property (and other properties) this time on the VisualBrush:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="BackgroundSkin.MainWindow"
    x:Name="Window"
    Width="640"
    Height="480"
    Background="{DynamicResource backgroundBrush}"    
>
    <Window.Resources>
        <!-- Replace the following Canvas with yours. -->
        <Canvas x:Key="backgroundCanvas"/>
        <VisualBrush
            x:Key="backgroundBrush"
            Visual="{DynamicResource backgroundCanvas}"
            Stretch="UniformToFill"
        />
    </Window.Resources>
</Window>
我不吻晚风 2024-07-25 08:36:32

首先,不要使用 Canvas 显式布局控件。 使用其他 Panel 类型(例如 GridDockPanel)。

其次,您可以像这样导入一个 ResourceDictionary

<Window>
    <Window.Resources>
        <ResourceDictionary Source="YourDictionary.xaml"/>
    </Window.Resources>
</Window>

或者您可以合并多个 ResourceDictionary,如下所示:

<Window>
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="YourDictionary.xaml"/>
                <ResourceDictionary Source="YourOtherDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

        <SolidColorBrush x:Key="SomeLocalResource">Red</SolidColorBrush>
    </Window.Resources>
</Window>

First of all, don't use a Canvas to lay out controls explicitly. Use the other Panel types (such as Grid and DockPanel).

Secondly, you can import a ResourceDictionary like this:

<Window>
    <Window.Resources>
        <ResourceDictionary Source="YourDictionary.xaml"/>
    </Window.Resources>
</Window>

Or you can merge in multiple ResourceDictionarys as follows:

<Window>
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="YourDictionary.xaml"/>
                <ResourceDictionary Source="YourOtherDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

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