【问题标题】:XAML - Dynamics set UriSource in BitmapIconXAML - 在 BitmapIcon 中动态设置 UriSource
【发布时间】:2015-01-01 00:02:18
【问题描述】:

我正在开发 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}"/> 

【问题讨论】:

    标签: c# xaml mvvm windows-phone-8.1 mvvm-light


    【解决方案1】:

    BitmapIcon.UriSource 的类型是 Uri 而不是 string。你应该改用这个:

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

    然后在您的视图模型中,您可以像这样设置图标:

    Icon = new Uri("ms-appx:///Assets/Main/Operations/appbar.information.png");
    

    This answer 解释了为什么在 XAML 中使用字符串有效但在 C# 代码中无效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 2013-05-04
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多