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 >
4

3 回答 3

1

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

于 2009-10-03T17:26:47.480 回答
1

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

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

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

或者你可以合并多个ResourceDictionarys,如下:

<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>
于 2009-04-22T18:36:00.407 回答
1

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

首先,您可以简单地在 Canvas 周围放置一个 Viewbox(您从转换后的 .ai 文件中获得)。请注意,您可能必须使用 Stretch 和 alignment 属性来使其按您希望的方式运行……但很可能您会将 Stretch 设置为 UniformToFill 并将对齐属性设置为 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>

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

如果您没有用于重新导出的原始 .ai/.design 文件,您可以从画布上创建一个 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>
于 2011-03-11T00:44:47.593 回答