我正在使用MediaPicker从设备相册中挑选照片。我想在我的应用程序的各个部分显示检索到的图像,并将其保存到sqlite数据库(作为项目的属性)。
任何帮助将不胜感激!
这三个类是相关的:
Model,“CodeItem.cs”实现了INotifyPropertyChanged:
公共类CodeItem:INotifyPropertyChanged { private static string imagepath {get;组; }
public static string ImagePath
{
get { return imagepath; }
set
{
imagepath = value;
//OnPropertyChanged("ImagePath");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private async void PickPhotoButton_OnClicked(object sender,EventArgs e)
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("Oh nein", "Pick photo is NotImplementedException supported!", "OK");
return;
}
var file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;
System.Diagnostics.Debug.WriteLine(file.Path);
await DisplayAlert("File Location", file.Path, "OK");
CodeItem.ImagePath = file.Path;
System.Diagnostics.Debug.WriteLine("Variable assigned: " + CodeItem.ImagePath);
}
xaml文件CodeItemPage:
<Image Source="{Binding ImagePath}">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="OnImageClicked"
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
<Label Text="{Binding ImagePath}"
FontSize="14"
TextColor="DarkGray"/>
错误是 - &gt; [0:]绑定:'CodeNote.CodeItem'上找不到'ImagePath'属性,目标属性:'Xamarin.Forms.Image.Source'
CodeItem.ImagePath = file.Path在执行方法 PickPhotoButton_OnClicked 时将file.Path的值分配给属性ImagePath。我仍然无法让图像显示出来。