如何在MVVM中的wpf中设置资源的镜像源

时间:2014-02-12 11:40:46

标签: c# wpf image mvvm

我想将图像源设置为资源中的图像。我有这个完美的xaml:

  <Image Source="/MyApplicationName;component/Resources/SplashScreen/InternetPlaceHolder.jpg" Height="577" Width="700" />

我想以一种在代码中加载图像的方式来更改它。所以我写了这段代码:

   <Image Height="577" Width="700"  x:Name="Image"/>


    MyImage = new Image();
    BitmapImage logo = new BitmapImage();
    logo.BeginInit();
    logo.UriSource = new Uri("pack://application:,,,/MyApplicationName;component/Resources/SplashScreen/InternetPlaceHolder.jpg");
    logo.EndInit();
    MyImage.Source = logo;

但它不起作用。

我检查过徽标并未从资源图片中初始化。显然uri有问题。我用我发现的文件检查了它,看起来它的格式是正确的。

我的问题:URI有什么问题?

1 个答案:

答案 0 :(得分:0)

首先,您必须将图片属性Build Action设置为Resource。然后尝试以下代码:

ImagePath = string.Format("{0}/{1}/{2}",AppDomain.CurrentDomain.BaseDirectory,"Resources/SplashScreen","InternetPlaceHolder.jpg");

XAML:

<Image  Source="{Binding ImagePath}" Height="577" Width="700" />
相关问题