gpt4 book ai didi

c# - XAML-动态在BitmapIcon中设置UriSource

转载 作者:行者123 更新时间:2023-12-03 10:36:16 25 4
gpt4 key购买 nike

我正在开发Windows Phone 8.1应用程序。如果我在BitmapIcon控件中设置UriSource,例如:

<BitmapIcon Width="35" Height="35" Margin="0" RequestedTheme="Dark" UriSource="/Assets/Main/Operations/appbar.information.png">

一切正常,但是如果我想对其进行动态设置,我会看到仍然是空字段。

我正在使用MVVM Light。我的模型如下:
    ...
private string _icon;
public string Icon
{
get { return _icon; }
set { _icon = value; RaisePropertyChanged("Icon"); }
}
...

接下来,我创建对象,添加到列表,然后在此基础上创建可观察的集合。其他字段正常工作,但不能这样:
<BitmapIcon Width="25" Height="25" Margin="0" UriSource="{Binding Icon, Mode=TwoWay}"/> 

最佳答案

BitmapIcon.UriSource的类型为Uri而不是string。您应该改用以下代码:

private Uri _icon;
public Uri Icon
{
get { return _icon; }
set { _icon = value; RaisePropertyChanged("Icon"); }
}

然后在 View 模型中,将图标设置如下:
Icon = new Uri("ms-appx:///Assets/Main/Operations/appbar.information.png");

This answer解释了为什么在XAML中使用字符串而不在C#代码中起作用的原因。

关于c# - XAML-动态在BitmapIcon中设置UriSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26746300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com