App.xaml Application.Resources 中的 WP7 绑定

发布于 2024-12-12 13:34:01 字数 860 浏览 0 评论 0原文

在 App.xaml 中,

我有:

<Color x:Key="ColorMain">#FF1F879C</Color>
<SolidColorBrush x:Key="ColorBrushMain" Color="{StaticResource ColorMain}"/>

然后我有许多使用此画笔和颜色的模板。这些模板在整个应用程序中使用。

我需要有能力改变颜色来改变整个应用程序的皮肤。 我需要类似:

<SolidColorBrush x:Key="ColorBrushMain" Color="{Binding ColorMain}"/>

并在代码中类似:

 public string ColorMain {
    get {
       return ..... ; // "#FF803200";
    }
 }

但它不起作用。请帮忙。

UPD:abhinav 是对的,它必须是一种颜色

 public Color ColorMain {
    get {
       return ..... ; // return Color.FromArgb(0xFF, 0x80, 0x32, 0x00);
    }
 }

,但这还不够,它没有约束力。我认为它必须与普通页面上的 DataContextViewModel 一样,但是什么呢?

In App.xaml <Application.Resources>

I have:

<Color x:Key="ColorMain">#FF1F879C</Color>
<SolidColorBrush x:Key="ColorBrushMain" Color="{StaticResource ColorMain}"/>

then I have many templates which are using this brush and color. These templates are used all over the application.

I need to have an ability to change the color to change the skin of whole application.
I need something like:

<SolidColorBrush x:Key="ColorBrushMain" Color="{Binding ColorMain}"/>

and in code something like:

 public string ColorMain {
    get {
       return ..... ; // "#FF803200";
    }
 }

But it doesn't work. Please help.

UPD: abhinav is right it must be a color

 public Color ColorMain {
    get {
       return ..... ; // return Color.FromArgb(0xFF, 0x80, 0x32, 0x00);
    }
 }

but this is not enough, it's not binding. I assume that it must be something as on normal page with DataContext to ViewModel, but what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开雨落又逢春i 2024-12-19 13:34:01

如果您要绑定到存储 Color 的属性,并且要在运行时更改并期望它更新,那么您是否也需要实现 INotifyPropertyChanged ?例如:

public class MyViewModel: INotifyPropertyChanged

    private Color _mainColor
    public Color MainColor
    {
        get { return _mainColor; }
        set
        {
            if (value != _mainColor)
            {
                _mainColor= value;
                NotifyPropertyChanged("MainColor");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

所以:如果您希望在运行时更改颜色,请使用绑定并实现 INotifyPropertyChanged - 如果颜色不会在运行时更改,那么您已经拥有的应该没问题。

If you're binding to a property that stores the Color and you're going to change at runtime and expect it to update, don't you need to be implementing INotifyPropertyChanged as well? For example:

public class MyViewModel: INotifyPropertyChanged

    private Color _mainColor
    public Color MainColor
    {
        get { return _mainColor; }
        set
        {
            if (value != _mainColor)
            {
                _mainColor= value;
                NotifyPropertyChanged("MainColor");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

So: if you're expecting to change the color at runtime, use binding and implement INotifyPropertyChanged - if the colour isn't going to change at runtime, what you've already got should be fine.

℡寂寞咖啡 2024-12-19 13:34:01

您正在将颜色属性绑定到字符串对象。
虽然我从未尝试过,但我很确定它不会起作用。

也许课程的文档会有所帮助。 查看此链接。

您是否尝试使用颜色类别呢?

    public Color ColorMain {
    get {
       return ..... ; // "#FF803200";
    }
 }

You're binding a color property to a string object.
Although I've never tried it, I'm quite sure that it will not work.

Maybe the documentation of the class will help. See this link.

Did you try using the color class instead?

    public Color ColorMain {
    get {
       return ..... ; // "#FF803200";
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文