2016-07-22 126 views
1

我試圖用{x:Static icons:New.png}代替C:\Users\StrangeUser\documents\visual studio 2012\Projects\WPFGO\WPFGO\Resources\New.png 但它不起作用New does not exists int namespace clr-namespace:WPFGO.Resources。如何解決它?WPF xaml源代碼路徑

的xmlns:圖標= 「CLR的命名空間:WPFGO.Resources」

<UserControl.Resources> 
     <Style x:Key="CreateIconButton"> 
      <Setter Property="Control.Template"> 
       <Setter.Value> 
        <ControlTemplate> 
         <!--<Image Source="{x:Static icons:New.png}"></Image>--> 
         <Image Source="C:\Users\StrangeUser\documents\visual studio 2012\Projects\WPFGO\WPFGO\Resources\New.png" Height="15" /> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </UserControl.Resources> 
+0

我必須失去了一些東西,你爲什麼不只是使用[包的URI(https://msdn.microsoft .COM/EN-US /庫/ aa970069(v = VS.100)的.aspx)? –

回答

2

一下添加到頂級資源標籤

<BitmapImage x:Key="somekey" UriSource="..." /> 

,然後你就可以做

<Image Source="{StaticResource somekey}"/> 
+0

但是如果目錄應該改變?它應該工作? – A191919

+0

@ A191919你將不得不更新UriSource,但你只需要做一次而不是遍佈整個地方。您也可以使用相對路徑。\ myImage.png – Steve

+0

資源是通過「{StaticResource key}」而不是「{x:Static key}」引用的。 –

0

我r成爲類似的問題,並想要一個更簡單的方法來管理這些資源。假設您要從外部VS控制文件目錄層次結構,您可以使用屬性文件。

  1. 在VS中轉到屬性 - >項目文件中的資源。
  2. 您需要指定一個名稱值(您將引用的內容),例如myResource,相關文件的實際位置,以及必要時的註釋。
  3. 繼續並保存該文件,並重建項目以使屬性與XAML同步。

現在你在你的項目中引用屬性,如本能力:

xmlns:prop="clr-namespace:WPFGO.Properties" 

<Image Source="{Binding Source={x:Static prop:Resources.myResource}}"/> 

整潔的部分這個將得到改變,在短短的資源文件的位置的能力。 XAML將爲您創建Uri,並使其作爲開發人員更簡單一些。

更新:

看起來你有足夠的資源文件夾下添加的資源,VS(WPFGO \資源\ New.png)。在性能部分的值可能是這個樣子:

/Resources/New.png