2016-03-18 20 views
4

我下載了Visual Studio Image Library,我看到包含的圖標以.XAML文件的形式可用於矢量格式。這裏有一個例子:將單獨的XAML導入爲ResourceDictionary並指定x:Key

Add_16x.xaml

<!-- This file was generated by the AiToXaml tool.--> 
<!-- Tool Version: 14.0.22307.0 --> 
<Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
    <Rectangle Width="16" Height="16"> 
    <Rectangle.Fill> 
     <DrawingBrush> 
     <DrawingBrush.Drawing> 
      <DrawingGroup> 
      <DrawingGroup.Children> 
       <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" /> 
       <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M5.0004,-0.000199999999999534L5.0004,4.9998 0.000399999999999956,4.9998 0.000399999999999956,10.9998 5.0004,10.9998 5.0004,15.9998 10.9994,15.9998 10.9994,10.9998 16.0004,10.9998 16.0004,4.9998 10.9994,4.9998 10.9994,-0.000199999999999534z" /> 
       <GeometryDrawing Brush="#FF388A34" Geometry="F1M10,6L15,6 15,10 10,10 10,15 6,15 6,10 1,10 1,6 6,6 6,1 10,1z" /> 
      </DrawingGroup.Children> 
      </DrawingGroup> 
     </DrawingBrush.Drawing> 
     </DrawingBrush> 
    </Rectangle.Fill> 
    </Rectangle> 
</Viewbox> 

我希望把所有的圖標XAML文件我想在我的應用程序源使用一個名爲「圖標」,然後文件夾中能有一個名爲IconDictionary.xaml的文件,它定義了一個ResourceDictionary,它擁有MergedDictionaries,在這個MergedDictionaries裏面,我希望能夠以某種方式包含icon.xaml文件併爲它們分配一個x:Key屬性,這樣我可以在整個應用程序中將它們作爲靜態資源引用。

是否可以在不修改.xaml文件本身的情況下使用此icon.xaml文件?

我希望能夠剛剛離開他們,因爲他們是可惜好像我別無選擇,只能要麼複製它們的內容到我的IconDictionary.xaml或編輯單個的.xaml文件,並圍繞他們在一個ResourceDictionaryx:Key,我可以添加到我的MergedDictionaries


UPDATE

這裏是我想什麼我IconDictionary.xaml看起來像爲清楚:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <DataTemplate x:Key="Add_16x"> 
     <!-- Import Viewbox from Add_16x.xaml here... --> 
    </DataTemplate> 

</ResourceDictionary> 

回答

2

是否可以在不修改.xaml文件本身的情況下使用此icon.xaml文件?

我不這麼認爲。雖然XAML看起來很像XML,並且遵循很多XML規則,但XAML編譯器似乎不允許DOCTYPEENTITY標記通常用於import XML from one file into another。在使用時,報告的第一個錯誤爲「在此XML文檔中禁止DTD」。如果沒有提供DTD的能力,則無法聲明實體,因此將無法導入獨立的XAML。


如果要直接使用生成的XAML文件,可以將該文件作爲資源添加到項目中。資源數據然後可以在代碼隱藏中使用XamlReader.Load()加載,並在適當的時候添加到您想要的地方。

例如,假設你複製的XAML文件名爲「資源」在你的項目文件夾,並設置「建設行動」的文件「資源」,您可以編寫代碼這樣的檢索對象:

using (Stream stream = App.GetResourceStream(
    new Uri("pack://application:,,,/Resources/Add_16x.xaml")).Stream) 
{ 
    object o = XamlReader.Load(stream); 
} 

這將讓代表已嵌入在你的應用程序,然後加載WPF XAML文件數據的Stream Object對象的XAML表示,使用XamlReader.Load()方法。個人而言,當我使用這些文件時,我只是將XAML文件中的DrawingGroup元素複製/粘貼到我自己的資源XAML文件中。無論如何,包裝(Viewbox,RectangleDrawingBrush)都是多餘的。我真正需要的是DrawingGroup的對象。

當我這樣做時,我也刪除了明確的<DrawingGroup.Drawing>語法,並且直接包含DrawingGroup的孩子,使得XAML更簡潔一些。例如:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <DrawingGroup x:Key="Add_16x"> 
    <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" /> 
    <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M5.0004,-0.000199999999999534L5.0004,4.9998 0.000399999999999956,4.9998 0.000399999999999956,10.9998 5.0004,10.9998 5.0004,15.9998 10.9994,15.9998 10.9994,10.9998 16.0004,10.9998 16.0004,4.9998 10.9994,4.9998 10.9994,-0.000199999999999534z" /> 
    <GeometryDrawing Brush="#FF388A34" Geometry="F1M10,6L15,6 15,10 10,10 10,15 6,15 6,10 1,10 1,6 6,6 6,1 10,1z" /> 
    </DrawingGroup> 
</ResourceDictionary> 

然後,您可以使用上述資源,只要您喜歡。例如,WPF中的慣用方法是聲明DataTemplate配置爲以您想要的方式顯示Drawing,然後將給定的Drawing資源綁定爲內容控件的內容。例如:

<DataTemplate DataType="{x:Type Drawing}"> 
    <Rectangle> 
    <Rectangle.Fill> 
     <DrawingBrush Drawing="{Binding}" Stretch="Uniform"/> 
    </Rectangle.Fill> 
    </Rectangle> 
</DataTemplate> 

然後在別處:

<Button Content="{StaticResource Add_16x}"/> 

(確保樣式的內容控制,例如,您的Button,與您Drawing資源,即正確的大小等,否則兼容)

+0

在看到替代品並不那麼友好之後,我最喜歡這個解決方案。我可以直接向'DrawingGroup'添加一個'x:Key',或者如果我想將它放在我的'ResourceDictionary'中,是否需要將它包裝在某些東西中? –

+0

您不僅可以將'x:Key'屬性添加到'DrawingGroup',如果您希望'DrawingGroup'對象被字典直接包含,您將被要求。更一般地說:你可以在ResourceDictionary集合中聲明一個_any_類型的對象,前提是對象有一個無參數的構造函數,並且ResourceDictionary的每個元素都需要有一個'x:Key'屬性。 –

+0

這也適用於僅包含「路徑」對象的XAML圖標嗎? –

0

這個怎麼樣?

的App.xaml

<Application x:Class="WpfApplication35.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:WpfApplication35" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <ControlTemplate x:Key="Add_16x"> 
      <!-- Import Viewbox from Add_16x.xaml here... --> 
     </ControlTemplate> 
    </Application.Resources> 
</Application> 

而且使用這樣的:

<ContentControl Template="{StaticResource Add_16x}" /> 

OR,

<Application x:Class="WpfApplication35.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:WpfApplication35" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <ContentControl x:Key="Add_16x"> 
      <!-- Import Viewbox from Add_16x.xaml here... --> 
     </ContentControl> 
    </Application.Resources> 
</Application> 

而且使用這樣的:

<Button Content="{StaticResource Add_16x}" /> 
相關問題