2014-09-28 64 views
1

我有一個具有特定功能的靜態類來提供資源路徑。目前我只是在所有視圖模型的綁定函數中創建包裝。我想知道在Windows Phone 8.1中是否有某種方式將靜態功能直接綁定到圖像源。 在wpf Objectdataprovider中會很有用,但它在windows store應用程序中不受支持。我也無法找到任何文檔。在Windows Store應用中使用靜態方法進行綁定

<Rectangle.Fill> 
<ImageBrush Stretch="Fill" 
      ImageSource="{Binding ImagePath"/> 
</Rectangle.Fill> 

在此先感謝

+0

你可以修改函數的屬性?如果是這樣,你可以綁定到像靜態屬性[this](http://stackoverflow.com/questions/936304/binding-to-static-property) – kennyzx 2014-09-28 08:30:59

+0

之前試過。得到這個錯誤「類型FooBar是抽象的,必須包含一個明確的值」 但我還沒有將類轉換爲非靜態。 – 2014-09-28 08:45:13

+0

爲什麼你使用函數而不是屬性有什麼特別的原因嗎? 'ImagePath'好像應該是屬性而不是函數。 – 2014-09-28 08:52:11

回答

1

它(目前)無法綁定到一個靜態類作爲綁定需要一個對象實例。但是,您可以綁定到類的靜態屬性。

你在你的靜態類創建一個實例包裝:

public class BindingHelper 
    { 
    public static string ImagePath 
    { 
     get { return AssetHelper.ImagePath; } 
    } 
    } 

現在爲這個BindingHelper創建的應用程序資源:

<application.resources> 
    <BindingHelper x:key="BindingHelperResource"></BindingHelper > 
</application.resources> 

,並使用該資源爲你的綁定:

<textblock text="{Binding Path=ImagePath, Source={StaticResource BindingHelperResource}}"> 
+0

我無法在windows phone 8.1中找到BindingHelper。 – 2014-09-28 09:14:21

+0

您創建BindingHelper類。它只是你靜態類的包裝。還要用實際的靜態類和靜態方法的名稱替換AssetHelper.Image路徑。 – Postlagerkarte 2014-09-28 09:23:59

+0

工作。我認爲這是因爲使用靜態函數,我得到錯誤。很高興知道。謝謝!! – 2014-09-28 22:01:08

相關問題