讓我首先說我已經試過this並查看了this(它只是導致第一個)。這些都是WPF的絕佳解決方案。正如我們都知道的那樣,Windows Phone 7缺少一些優秀的優勢案例。將圖像設置爲WP7上的本地資源
我的項目似乎註定要缺乏動態圖像。我有一個標籤,我試圖綁定到一個資源文件,它不工作。我已經嘗試了以下兩種方法,但都沒有成功。
首先,流行的轉換器。獲取URI字符串並返回位圖圖像。顯然它在WPF中工作,但我無法使它工作。
public class BitmapImageConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
try
{
return new BitmapImage(new Uri((string)value));
}
catch
{
return new BitmapImage();
}
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
然後是BitmapImage屬性。沒有真正期待這個工作,它並沒有令人失望。
public BitmapImage ImgSource
{
get
{
// ImgURI is a URI to the image.
BitmapImage newImg = new BitmapImage(new Uri(ImgURI));
// Tried ALL the creation options. :(
//newImg.CreateOptions = BitmapCreateOptions.None;
return newImg;
}
}
這是我使用的轉換器(這是其他地方所定義)是XAML ....
<Image Name="imgMap"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Source="{Binding ImgURI, Mode=OneWay, Converter={StaticResource bitmapConverter}}"
Stretch="None"/>
我把它調試到如此地步,我知道,每個綁定返回它應該是什麼。轉換器被調用,並做我認爲應該做的。 ImgURI總是返回一個格式爲「/MyNamespace;component/Images/MyImg.jpg」的字符串,ImgSource總是一個bitmapImage。所有圖像都有一個BuildAction的「資源」。
我希望我失去了一些明顯的東西。感謝您查看我的問題,如果需要更多信息,請發表評論。
這些資源與消費Image相同嗎?這在仿真器和設備上都失敗了?轉換器是否會引發異常或其他問題? – 2011-06-13 09:36:45
是的。是。轉換器工作正常,沒有錯誤,例外或逃避。很奇怪,呵呵。 – Craig 2011-06-13 18:14:52