【问题标题】:Load image from another assembly's resx file in XAML在 XAML 中从另一个程序集的 resx 文件加载图像
【发布时间】:2012-03-14 20:22:11
【问题描述】:

我有两个程序集,比如程序集 1 和程序集 2。

在 assembly2 中有一个 XAML 文件。在这个 XAML 文件中,我想创建一个图像。

我想要做的是将此图像的源设置为来自 assembly1 的 resx 文件中的位图。

<Image Name="image1" Stretch="Fill" Source="???" />

如何在 XAML 中正确引用该位图文件?是否有简单的“仅 XAML”解决方案?

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    好的,所以我假设没有诸如“仅 XAML”的解决方案之类的东西。

    相反,我在调用 WPF 控件的 Loaded 事件后这样做:

    Assembly coreAssembly = Assembly.GetAssembly(typeof (otherAssembly.Resources));
    var resourceManager = new ResourceManager("otherAssembly.Resources", coreAssembly);
    
    // get image from core resources
    Bitmap completeImage = (Bitmap) resourceManager.GetObject("Complete");
    
    // apply image to WPF image
    var memoryStream = new MemoryStream();
    completeImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
    bitmapImage.EndInit();
    
    this.myWpfImage.Source = bitmapImage;
    

    【讨论】:

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