简体   繁体   English

无法绑定到 ResourceDirectory WinUI3/UWP 中的公共属性

[英]Cannot bind to public property inside ResourceDirectory WinUI3/UWP

So I am currently trying to bind a public static SolidColorBrush to a "Filled" property on a Rectangle inside a ResourceDirectory Style (inisde App.xaml).因此,我目前正在尝试将公共静态 SolidColorBrush 绑定到 ResourceDirectory 样式 (inisde App.xaml) 中矩形上的“填充”属性。 But I always get the Exception但我总是得到例外

"Type 'x' used after '{' must be a Markup Extention. Error code 0x09c6." “在‘​​{’之后使用的类型‘x’必须是标记扩展。错误代码 0x09c6。”

My xaml code looks like this (inside App.xaml):我的 xaml 代码如下所示(在 App.xaml 中):

<Rectangle
    x:Name="SelectionIndicator"
    Width="4"
    Height="24"
    Fill="{x:Bind SomeNamespace:SomeClass.SomeColor}"/>

and my codebehind looks like this (inside public class called "SomeClass" in Namespace called "SomeNamespace"):我的代码隐藏看起来像这样(在名为“SomeNamespace”的命名空间中名为“SomeClass”的公共类中):

public static SolidColorBrush SomeColor = new(Colors.Red);

I know its possible to bind to a public static property with x:Bind in WinUI/UWP but this somehow doesnt work.我知道它可以在 WinUI/UWP 中使用 x:Bind 绑定到公共静态属性,但这在某种程度上不起作用。 I really need to bind from some code behind so I can implement INotifyPropertyChanged so I cant just create a new color resource.我真的需要从后面的一些代码中绑定,这样我才能实现 INotifyPropertyChanged,所以我不能只创建一个新的颜色资源。 What is the correct way to implement my wanted behavior?实现我想要的行为的正确方法是什么? Thanks in advance!提前致谢!

You probably can't call your PropertyChanged event handler from a static property.您可能无法从静态属性调用 PropertyChanged 事件处理程序。 I think you'll need an instance.我想你需要一个实例。

Instead, consider storing the brush as a non-static property in a wrapper class.相反,请考虑将画笔作为非静态属性存储在包装类中。 You can instantiate this class as a resource in your App.xaml file and use it across your application.您可以将此类实例化为 App.xaml 文件中的资源,并在您的应用程序中使用它。

ColorStorer is a class to store the SolidColorBrush: ColorStorer 是一个存储 SolidColorBrush 的类:

public class ColorStorer: INotifyPropertyChanged
{
    private SolidColorBrush scb = new SolidColorBrush(Colors.Red);
    public SolidColorBrush Scb
    {
        get
        {
            return scb;
        }

        set
        {
            if (value as SolidColorBrush != null)
            {
                scb = value;
                NotifyPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged([CallerMemberName] string name = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

Instantiate it in Application.Resources in App.xaml:在 App.xaml 的 Application.Resources 中实例化它:

<Application.Resources>
    <ResourceDictionary>
        <local:ColorStorer x:Key="TestAppColorStorer" />
    </ResourceDictionary>
</Application.Resources>

Use it in your views and controls:在您的视图和控件中使用它:

<Rectangle
            Width="100"
            Height="50"
            HorizontalAlignment="Center"
            Fill="{Binding Scb, Source={StaticResource TestAppColorStorer}, Mode=OneWay}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM