以下是一個完全有效的XAML:
< Image Width="50" Height="50" Source="/Assets/StoreLogo. png" />
問題是,UWP如何知道將這個字串轉換為ImageSource。我期待看到類似IValueConverter介面或CreateFromString屬性的東西。
但是這些是我能看到的唯一屬性:
uj5u.com熱心網友回復:
UWP的XAML如何識別ImageSource從字串的轉換
ImageSource是影像控制元件的依賴屬性,這意味著你可以在PropertyChangedCallback方法中把它轉換成BitmapImage。
當源屬性改變時,它將呼叫回呼方法,你可以像下面這樣用字串url制作BitmapImage。 為了解釋這一點,你可以制作一個自定義的依賴性屬性。
imageHolder.Source = new BitmapImage(new Uri("ms-appx://local/test.png"));
依賴性
public static readonly DependencyProperty UriProperty = DependencyProperty.Register(
"Uri",
typeof(String),
typeof(CustomImage),
new PropertyMetadata(null,new PropertyChangedCallback(OnUriChanged))
);
請注意Image是密封的型別,你不能讓一個類從它派生出來
。uj5u.com熱心網友回復:
硬編碼的影像路徑要在編譯時轉換為位圖,所以在運行時不滿足String-to-ImageSource的轉換。對于通過{x:Bind}動態指定的路徑字串,UWP在后面的代碼中使用XamlBindingHelper.ConvertValue()(obj檔案夾中自動生成的*.g.cs檔案)。也許XAML分析器內部也會使用類似的東西。如果你想采取同樣的方法,請嘗試以下代碼:
theImage.Source = XamlBindingHelper. ConvertValue(typeof(ImageSource), "/Assets/StoreLogo.png") as ImageSource;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/312738.html
標籤:

