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

时间:2017-06-22 16:09:51

标签: binding xamarin.forms

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

任何帮助将不胜感激!

这三个类是相关的:

  1. 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));
    }
    
    • 视图CodeItemPage的支持类,它包含选择图像按钮的侦听器,并且应该将ImagePath的值设置为所选图像的file.Path:

    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);
    
    }
    
  2. 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。我仍然无法让图像显示出来。

0 个答案:

没有答案