如何在xaml中定义和使用资源,以便它们可以在C#中使用

mar*_*000 18 c# wpf resources xaml

从理论上讲,我认为我可以在xaml文件中定义画笔和颜色等,并将其分配给c#中的button.background.但是我该怎么做?我在哪里放置如下的lineargradientbrush定义:

<LinearGradientBrush x:Key="BlaBrush">
                <GradientStop Offset="0" Color="Red"/>
                <GradientStop Offset="1" Color="Green"/>
</LinearGradientBrush>
Run Code Online (Sandbox Code Playgroud)

只是将它放在我窗口的xaml文件中的不同位置会导致各种错误消息:/

我发现这里的计算器这样一个问题:如何在XAML中使用定义的画笔资源,从C#这也解释了其中的一部分,但他似乎知道在哪里做刷机的定义.

我还尝试将shinyblue.xaml wpf模板添加<ResourceDictionary Source="ShinyBlue.xaml"/>到应用程序并添加到app.xaml中的application.resources.这使我的所有按钮的蓝色瞬间,不过,在"东西"像NormalBrush shinyblue.xaml定义不是从C#访问-至少我不知道怎么办.

JSp*_*ang 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>
Run Code Online (Sandbox Code Playgroud)

要分配值,您需要从以下资源中获取渐变画笔:

MainWindow.xaml.cs

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


Bri*_*sio 16

请注意,现有的答案谈到将资源放在Window.Resources中.如果您希望资源在整个应用程序范围内可用,您可以考虑将它们放在App.xaml或更好的位置,创建可以包含在视图中并在其他地方重复使用的独立资源字典(包括其他项目)

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="DefaultStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <Style x:Key="my_style" />
    </ResourceDictionary>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)


Qua*_*ter 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>
Run Code Online (Sandbox Code Playgroud)

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

var blaBrush = this.FindResource("BlaBrush") as LinearGradientBrush;
Run Code Online (Sandbox Code Playgroud)

有关更多信息, 请参阅参考资料


Oza*_*zan 8

您可以访问应用程序资源

Application.Current.Resources["BlaBrush"] as LinearGradientBrush
Run Code Online (Sandbox Code Playgroud)

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