【问题标题】:Bind xaml vector images dynamically动态绑定 xaml 矢量图像
【发布时间】:2010-05-10 02:15:31
【问题描述】:

我正在寻找一种在代码中动态绑定 xaml 图像的方法。

网上有很多例子展示了如何在窗口的xaml中绑定png或xaml图片,或者在代码中加载png文件。

但是我没有找到带有 build action=page 的 xaml 文件的示例。 XmlReader 无法处理它们,因为它们被编译为 baml。

LoadComponent(new Uri("...filename.baml") 显然会加载 baml 但我应该将它加载到什么样的图像类中? BitmapImage(new Uri("...filename.baml") 不会似乎可以工作,可能是因为它不是位图。

提前致谢。

【问题讨论】:

    标签: xaml image binding vector


    【解决方案1】:

    此外,如果您的图像以这种格式存储在 Xaml 矢量文件中:

    您可以通过以下方式加载单个 xaml 矢量图像(最好防止字典被多次添加,如果您像我一样,通过 OpenFileDialog 加载它):

    C#:

    string fullPathAndFileName = "C:\Image_Name.xaml"; 资源字典 dict = new ResourceDictionary(); dict.Source = new Uri(fullPathAndFileName); Application.Current.Resources.MergedDictionaries.Add(dict);

    DrawingImage image = dict[dict.Keys.Cast().ToList()[0]] as DrawingImage; myImage.Source = 图像;

    XML:

    图像高度="200" 宽度="200" x:Name="myImage"

    【讨论】:

      【解决方案2】:

      经过反复试验和搜索,以下文章帮助了我: Access ResourceDictionary items programmatically

      如果每个 Xaml 矢量图像都包含在带有键的 ResourceDictionary 中(有关格式,请参阅下面我的第二篇文章)...

      如果您的 Xaml 矢量图像文件都存储在您的项目中(构建操作:页面),您可以通过以下方式加载它们:

      //Get the ResourceDictionary using the xaml filename:
      ResourceDictionary dict = System.Windows.Application.LoadComponent(new Uri("/yourprojectname;component/youriconfolder/youriconfilename.xaml", System.UriKind.Relative)) as ResourceDictionary;
      
      //Get the xaml as a DrawingImage out the ResourceDictionary 
      DrawingImage image = dict[dict.Keys.Cast<object>().ToList()[0]] as DrawingImage;
      

      我可以返回图像并将其绑定到返回 iconfilename.xaml 的 viewmodel 属性。然后我使用带有上述代码的转换器来查找图标并将其作为 DrawingImage 返回。 或者您可以将其指定为图像的来源(请参阅下面我的第二篇文章)。

      更新:2015.10.08 - Carl 似乎没有解决他帖子的“XAML 格式示例”部分。 XAML 看起来像这样:

      <ResourceDictionary
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <DrawingImage x:Key="SomeUniqueKey">
          <DrawingImage.Drawing>
              <DrawingGroup>
                  ...
              </DrawingGroup>
          </DrawingImage.Drawing>
      </DrawingImage>
      </ResourceDictionary>
      

      那么你可以这样做:

      DrawingImage image = dict["SomeUniqueKey"] as DrawingImage;
      

      或者,如果您更喜欢直接使用Image

      <ResourceDictionary
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Image x:Key="SomeUniqueImage">
          <Image.Source>
              <DrawingImage>
                  ...
              </DrawingImage>
          </Image.Source>
      </Image>
      </ResourceDictionary>
      

      还有:

      Image someImage = dict["SomeUniqueImage"] as Image;
      

      【讨论】:

        猜你喜欢
        • 2011-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-16
        相关资源
        最近更新 更多