Binding Forms:如何加载file.path并将其保存到sqlite数据库

Binding Forms:如何加载file.path并将其保存到sqlite数据库,binding,xamarin.forms,Binding,Xamarin.forms,我正在使用MediaPicker从设备相册中拾取照片。我希望在应用程序的各个部分显示检索到的图像,并将其保存到sqlite数据库(作为项目的属性) 任何帮助都将不胜感激 这三类是相关的: 实现INotifyPropertyChanged的模型“CodeItem.cs”: 公共类代码项:INotifyPropertyChanged { 私有静态字符串imagepath{get;set;} public static string ImagePath { get { return image

我正在使用MediaPicker从设备相册中拾取照片。我希望在应用程序的各个部分显示检索到的图像,并将其保存到sqlite数据库(作为项目的属性)

任何帮助都将不胜感激

这三类是相关的:

  • 实现INotifyPropertyChanged的模型“CodeItem.cs”:

    公共类代码项:INotifyPropertyChanged { 私有静态字符串imagepath{get;set;}

    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));
    }
    
    • view CodeItemPage的支持类,其中包含“选择图像”按钮的侦听器,并应将ImagePath的值设置为文件。所选图像的路径:
    私有异步void PickPhotoButton_OnClicked(对象发送方,事件参数e)

  • xaml文件代码项页面:

                    <Image Source="{Binding ImagePath}">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer
                Tapped="OnImageClicked"
                NumberOfTapsRequired="1" />
                    </Image.GestureRecognizers>
                </Image>
    
                <Label  Text="{Binding ImagePath}"
                        FontSize="14"
                        TextColor="DarkGray"/>
    
    
    
    • 错误为-->[0:]在“CodeNote.CodeItem”的目标属性“Xamarin.Forms.Image.Source”上未找到绑定:“ImagePath”属性

    • CodeItem.ImagePath=file.Path在执行PickPhotoButton_OnClicked方法时,会将file.Path的值指定给属性ImagePath。但我仍然无法显示图像


    您不应该在同一元素上同时使用绑定和显式赋值。选择其中一个。BindingContext是CodeItem吗?如果是,只需将ImagePath属性分配给file.Path,并让绑定为您处理UI更新。然后,当您要保存到db时,将设置您的值。好的,这就是我的想法是的,CodeItem是BindingContext。我设置了
    CodeItem.ImagePath=file.Path;
    ,但图像不会显示。当我手动键入路径到:
    时,它仍然会显示。有什么想法吗?谢谢你的帮助!CodeItem需要实现INotifyPropertyChanged,这就是绑定UI收到通知的方式在上面的数据中,我添加了INotifyPropertyChanged的实现,但不知何故,我仍然无法设置codeem.ImagePath=file.path;--您将如何进行设置?“我仍然无法设置”-这意味着什么?您是否收到错误或异常?您试图在哪里设置它?
                    <Image Source="{Binding ImagePath}">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer
                Tapped="OnImageClicked"
                NumberOfTapsRequired="1" />
                    </Image.GestureRecognizers>
                </Image>
    
                <Label  Text="{Binding ImagePath}"
                        FontSize="14"
                        TextColor="DarkGray"/>