這裏是MVVM綁定圖像資源到圖像控件的完整實現。您需要將您的視圖模型設置爲XAML所在頁面的上下文。另外訪問「App2.fb.png」似乎很奇怪,它應該只是fb.png。這可能是一個簡單的修復..只是重命名圖像源的圖像的確切名稱的Droid>資源
XAML上市
<Image
Aspect="AspectFit"
Source="{Binding PropertyImageStatusSource}">
基本視圖模型
有你viewmodels從viewmodel基類繼承,因此INotifyPropertyChanged在你的存取器中被普遍實現。
public class _ViewModel_Base : INotifyPropertyChanged
{
//figure out what is getting set
public virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
//attach handler with property name args to changing property, overridable in inheriting classes if something else needs to happen on property changed
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
視圖模型
Public MyViewModel : _ViewModel_Base
{
private string ImageStatusSource = "fb.png";
public string PropertyImageStatusSource
{
set { SetProperty(ref ImageStatusSource, value); }
get { return ImageStatusSource; }
}
}
你說你的圖像被稱爲fb.png,但在你的代碼是稱它爲「App2.fb.png」。您需要在ImageSource.FromResource –