如何在XAML中定义和使用资源,以便在C#中使用

18

理论上,我认为我可以在 xaml 文件中定义笔刷和颜色等,并将其分配给 C# 中的 button.background。但是我该如何做呢?我应该在哪里放置我的线性渐变笔刷定义,例如:

<LinearGradientBrush x:Key="BlaBrush">
                <GradientStop Offset="0" Color="Red"/>
                <GradientStop Offset="1" Color="Green"/>
</LinearGradientBrush>

我只是在我的窗口XAML文件的各个位置添加它,结果会得到各种错误消息 :/

我在stackoverflow上找到了这个问题:如何在C#中使用XAML中定义的画刷资源,其中解释了一部分,但他似乎知道在哪里定义Brush。

我还尝试将shinyblue.xaml wpf模板添加到应用程序中,并在app.xaml中的application.resources中添加<ResourceDictionary Source="ShinyBlue.xaml"/>。这使得我的所有按钮立即变成蓝色,但是,像NormalBrush之类的在shinyblue.xaml中定义的“东西”无法从C#中访问 - 至少我不知道怎么做。

Marc


1
我想把你的所有帖子都选为答案,但我不能:D 谢谢你提供的所有信息。它对我很有帮助。 - marc40000
4个回答

18
您的XAML代码应该长这样:

MainWindow.xaml

<Window x:Class="BrushResource.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <LinearGradientBrush x:Key="BrushOne" StartPoint="0,0.5" EndPoint="1,0.5" Opacity="0.5">
        <LinearGradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="Black" Offset="0" />
                <GradientStop Color="Silver" Offset="1" />
            </GradientStopCollection>
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="BrushTwo" StartPoint="0,0.5" EndPoint="1,0.5" Opacity="0.5">
        <LinearGradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="Maroon" Offset="0" />
                <GradientStop Color="Silver" Offset="1" />
            </GradientStopCollection>
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>
</Window.Resources>

<StackPanel>
    <Button Content="Button" Width="100" Click="myButton_Click"/>
</StackPanel>

要赋值,需要从资源中获取梯度画刷,方法如下:

MainWindow.xaml.cs

private void myButton_Click(object sender, RoutedEventArgs e)
    {
        (sender as Button).Background = this.Resources["BrushOne"] as LinearGradientBrush;
    }

如果我有一个 Window 的子类,Window.Resources 部分会识别它吗?还是我必须在那里明确列出我的子类? - sproketboy

16
请注意,现有的答案讨论将资源放在Window.Resources中。如果您希望这些资源在整个应用程序中都可用,可以考虑将它们放置在App.xaml中,或者更好的做法是创建独立的资源字典,可以在您的视图中包含并在其他地方重复使用(包括其他项目)。

请注意,现有的答案讨论将资源放在 Window.Resources 中。如果您希望这些资源在整个应用程序中都可用,可以考虑将其放置在 App.xaml 中,或者更好的做法是创建独立的资源字典,可以在您的视图中包含并在其他地方重复使用(包括其他项目)。

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="DefaultStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <Style x:Key="my_style" />
    </ResourceDictionary>
</UserControl.Resources>

我该怎么做?当我尝试在xaml中将多个<ResourceDictionary Source="ShinyBlue.xaml"/>行添加到Application.Resources中时,会出现错误提示我已经定义了它。将其放入windows.resources中有点类似。当我在那里添加自己的xaml文件时,它会忘记shinyblue.xaml中定义的“东西”? - marc40000

13
将它们放在 XAML 元素的 Resources 集合中:
<Window ...>
    <Window.Resources>
        <LinearGradientBrush x:Key="BlaBrush">
            <GradientStop Offset="0" Color="Red"/>
            <GradientStop Offset="1" Color="Green"/>
        </LinearGradientBrush>
        <!-- Other resources -->
    </Window.Resources>
    <!-- Contents of window -->
</Window>

然后使用FindResource在代码中获取它们。

var blaBrush = this.FindResource("BlaBrush") as LinearGradientBrush;

查看 资源概述 以获取更多信息。

8

您可以通过以下方式访问应用程序资源:

Application.Current.Resources["BlaBrush"] as LinearGradientBrush

或者,您可以将资源添加到控件的资源中,并像Quartermeister所写的那样访问它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接