2016-09-16 20 views
0

我想通了如何從一個SVG文件(.svg-> Inkscape-> PDF - > AI-> ExpressionDesign-> XAML)得到一個XAML任何清潔方式。是否有使用通過包網址矢量圖像WPF/XAML中

轉換要麼給我一個DrawingBrush或用帆布XAML文件資源字典。

現在我在尋找通過包URL中使用的矢量圖像,所以我可以在從我的ViewModel一個乾淨的方式來使用它乾淨的方式。這是一個xaml片段,可以很好地與ImagePath(字符串)一起使用,該ImagePath包含包(URL)到一個(資源).png文件。矢量圖像有什麼相似之處嗎?

// View Model: 
MainMenuEntry.ImagePath = "pack://application:,,,/MyBeautifulApp.Wpf.MainGui;component/CommonResources/OpenFile16x16.png" 

// .Xaml File 
<DataTemplate> 
    <Button Command ="{Binding ClickCommand}" Margin="3,0,0,0"> 
     <Button.Template> 
      <ControlTemplate> 
       <Image Source="{Binding ImagePath}" Width="16" Height="16" Stretch="Uniform" VerticalAlignment="Center" /> 
      </ControlTemplate> 
     </Button.Template> 
    </Button> 
</DataTemplate> 

只有這樣,我發現它在所有與綁定的ImageSource的工作是與DrawingBrush(以ResourceDictionary中)一個靜態資源,但在那裏我可以不幫我視圖模型有一個位圖文件或矢量圖像。必須有任何理智的方式來處理矢量圖像像使用html中的svg?

+0

不確定你的意思是「乾淨」的方式。我這樣做的方式是AI - > [XAML轉換](http://www.mikeswanson.com/xamlexport/) - >資源。如果它是一個單一的路徑,那麼我只是把它做成一個Style模板。如果它是多個我使用[不同的模板](http://stackoverflow.com/questions/13292179/best-way-to-use-a-vector-image-in-wpf/13293017#13293017)方法。然後,它全部放在資源字典中,並通過StaticResource調用,無論是來自XAML還是代碼隱藏,都像魅力一樣。 –

+0

隨着清潔我的意思簡單的僞像「包://應用:,,,/MyBeautifulApp.Wpf.MainGui;組件/ CommonResources/OpenFileVectorGraphic.xaml」,我可以綁定就像PNG。猜猜我現在將爲viewmodel製作一個MenuItemCommand類,它具有多個位圖/矢量圖標屬性或者一個對象屬性和一個樣式/模板,可以根據屬性返回的類型動態構建適當的項目(packpath string/xaml graph )。 –

+0

嗯,如果你真的想要使用Image.Source,你可以將你的XAML Vector移植到DrawingImage資源中,並且仍然可以執行'' –

回答

-1

出於組織目的,爲您的資產創建單獨的類庫項目。定義你的XAML矢量圖形在ResourceDictionary中

資產/分類/我-asset.xaml:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d"> 
<VisualBrush x:Key="my-asset" Stretch="Uniform"> 
    <VisualBrush.Visual> 
     <Canvas Width="48.822" Height="53.243"> 
       <Path {...} /> 
      </Canvas> 
     </VisualBrush.Visual> 
    </VisualBrush> 
</ResourceDictionary> 

定義一個包含所有資產的參考資源字典:

資產/資產。 XAML

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" > 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="Category/my-asset.xaml" /> 
     {...} 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 

在你的App.xaml加載Assets.xaml

<Application x:Class="MyApp.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="pack://application:,,,/ClientAssets;component/Assets/Assets.xaml" /> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 

有了這個功能,你可以在任何你喜歡的地方使用你的圖形作爲長方形的畫筆。

<Rectangle Grid.Row="2" Fill="{StaticResource my-asset}" Height="16" Width="16" />