0

我在 xaml 中使用图像,如下所示,

<Image x:Name="JustMyImage" Width="635" Height="120" Canvas.Left="-19" Canvas.Top="-19" Source="../images/UnCategorized/Anywhere.png"/>

如果我想动态更改图像源,我需要执行以下代码

 BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri(".\images\panel.PNG", UriKind.Relative);
            bi.EndInit();

            this.JustMyImage.Source = bi;

是否有直接的“一条线”方法来替换图像。

4

3 回答 3

0

你可以这样做:

 JustMyImage.Source = new BitmapImage(new Uri(".\images\panel.PNG"))
于 2012-08-06T05:01:41.663 回答
0

您可以在资源中创建一个 BitmapImage 标记并引用它(我认为 StaticResource 会起作用,否则使用 DynamicResource)。

从您的“JustMyImage”中引用此 BitmapImage。当您更新 BitmapImage 的 URI 时,“JustMyImage”应反映此更改。

于 2012-08-06T07:04:32.647 回答
0

我使用转换器来解决这个问题:

public class KepPathKonverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        BitmapImage o;
        try
        {
            o = new BitmapImage(new Uri($"{Main.baseDir}\\{value}"));
        }
        catch (Exception ex)
        {
            o = new BitmapImage();
        }
        return o;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
}
于 2018-08-01T08:33:38.637 回答