【问题标题】:How to define and use resources in xaml so they can be used in C#如何在 xaml 中定义和使用资源,以便它们可以在 C# 中使用
【发布时间】:2011-03-19 13:00:35
【问题描述】:

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

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

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

我在 stackoverflow 上发现了这个问题:How to use a defined brush resource in XAML, from C# 解释了其中的一部分,但他似乎知道在哪里进行 Brush 定义。

我还尝试将 shinyblue.xaml wpf 模板添加到应用程序,并将 &lt;ResourceDictionary Source="ShinyBlue.xaml"/&gt; 添加到 app.xaml 中的 application.resources。这使我的所有按钮立即变为蓝色,但是,在 shinyblue.xaml 中定义的“事物”(如 NormalBrush)仍然无法从 C# 访问 - 至少我不知道如何访问。

马克

【问题讨论】:

  • 我想选择你所有的帖子作为答案,但我不能:D 感谢所有这些信息。这对我帮助很大。

标签: c# wpf xaml resources


【解决方案1】:

您可以访问应用程序资源

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我该怎么做?当我尝试在 xaml 中向 Application.Resources 添加多个 行时,我收到一条错误消息,告诉我它已被定义。把它放在 windows.resources 有点类似。当我在那里添加自己的 xaml 文件时,它以某种方式忘记了 shinyblue.xaml 中定义的“事物”?
    【解决方案3】:

    您的 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 部分会认识到这一点,还是我必须在那里拼出我的子类?
    【解决方案4】:

    将它们放在 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;
    

    请参阅Resources Overview 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      相关资源
      最近更新 更多