2014-02-05 98 views
2

我試圖用XamlReader在Image.Source中顯示我的矢量圖像。我有這樣的XAML資源。WPF使用XAML圖像源

<Canvas Width="76" Height="76" ClipToBounds="True" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
<Path Fill="#FF000000" Height="76" Stretch="Fill" Width="76"> 
    <Path.Data> 
     <PathGeometry FillRule="Nonzero" Figures="M21,30.0001L55.9999,30.0001 55.9999,50 21,50 21,30.0001z M52,28L37,28C38,25,39.4999,24.0001,39.4999,24.0001L50.75,24C51.3023,24,52,24.6977,52,25.25L52,28z" /> 
    </Path.Data> 
</Path> 

創建從here的結合。

<Image Stretch="Fill" Source="{Binding Converter={StaticResource uriToUIElementConverter},ConverterParameter=images/Folder.xaml}"/> 

該文件的屬性Build Action=Resource:可是,當我嘗試用用它不起作用。該轉換器uriTOUIElementConverter是:

public class FileToUIElementConverter :IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     FileStream fileStream = new FileStream((string)parameter, FileMode.Open); 
     return XamlReader.Load(fileStream) as DrawingImage; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

當我嘗試建立項目它給了我這些錯誤:

System.IO.FileNotFoundException 

我編輯器這樣的:

Stream fileStream = Application.GetResourceStream(new Uri("pack://application:,,,/ASSEMBLYNAME;component/"+(string) parameter)).Stream; 

但它不工作再次。我該怎麼做才能使它工作?

回答

4

您需要提供的路徑Application.GetResourceStream與應用程序包有關。

實施例:

我有XAML文件圖像/ Folder.xaml。 Folder.xaml的構建操作是資源

Folder.xaml

<DrawingImage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
    <DrawingImage.Drawing> 
     <GeometryDrawing Brush="LimeGreen"> 
      <GeometryDrawing.Geometry> 

       <PathGeometry FillRule="Nonzero" 
           Figures="M21,30.0001L55.9999,30.0001 55.9999,50 21,50 21,30.0001z M52,28L37,28C38,25,39.4999,24.0001,39.4999,24.0001L50.75,24C51.3023,24,52,24.6977,52,25.25L52,28z" /> 

      </GeometryDrawing.Geometry> 
     </GeometryDrawing> 
    </DrawingImage.Drawing> 
</DrawingImage> 

轉換器:

public class FileToUIElementConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     string path = parameter.ToString(); 

     StreamResourceInfo sri = Application.GetResourceStream(new Uri(path, UriKind.Relative)); 
     if (sri != null) 
     { 
      using (Stream stream = sri.Stream) 
      { 
       var logo = XamlReader.Load(stream) as DrawingImage; 

       if (logo != null) 
       { 
        return logo; 
       } 
      } 
     } 

     throw new Exception("Resource not found"); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

用法:

<Image x:Name="ImageLogo" Source="{Binding Converter={StaticResource FileToUiElementConverter}, ConverterParameter=images/folder.xaml}"/>