【问题标题】:how to set the image source from resource in wpf in MVVM如何在 MVVM 中从 wpf 中的资源设置图像源
【发布时间】:2014-03-10 17:16:52
【问题描述】:

我想将图像源设置为资源中的图像。我有这个完美运行的 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 有什么问题?

【问题讨论】:

  • 想一想:您是否尝试过使用带有UriKind 的重载Uri 构造函数?您可以尝试指定UriKind.Absolute。不确定它是否会有所作为,但值得一试。
  • 谢谢,已经测试过了,但是不行!
  • logo.UriSource = new Uri("/MyApplicationName;component/Resources/SplashScreen/InternetPlaceHolder.jpg", UriKind.RelativeOrAbsolute); -- 试试这个...

标签: c# wpf image mvvm


【解决方案1】:

首先,您必须将图像属性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" />

【讨论】:

    猜你喜欢
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多