首页 > 解决方案 > 使用firebase上传图片的方法出错

问题描述

当我要在 Firebase 中的数据库中上传图像时,我遇到了问题。

这是我的视图 xaml 称为:InitialPage

      <StackLayout Grid.Row="0">
            <Image Source="{Binding Icon}"/>

            <Button Text="Add Image"
                    Command="{Binding ImageDataCommand}"/>
        </StackLayout>

我有一个名为 ImageDataCommand 的按钮,当我选择在电话库中搜索图像时,会显示错误

Icon.Source

        private string _icon;
        public string Icon
        {
            get { return _icon; }
            set
            {
                _icon = value;
                RaisePropertyChanged();
            }
        }

        public ICommand ImageDataCommand => new Command(async (s) => await InsertImage(s));

        private async Task InsertImage(object s)
        {
            await CrossMedia.Current.Initialize();
            try
            {
                file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions()
                {
                    PhotoSize = PhotoSize.Medium
                });
                if (file == null)
                    return;

                Icon.Source = ImageSource.FromStream(() =>
                {
                   var imageStream = file.GetStream();
                    return imageStream;
                });

            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex);
            }
        }

我正在使用 FreshMvvm,我看到一种方法是使用 x:name 但在我的 InitPageModel 中未显示。

我开始使用 Freshmvvm 来避免将代码放入类或视图的 .cs 文件中。

问候

标签: xamarinxamarin.formsxamarin.ios

解决方案


  1. Icon从更改stringImageSource
private ImageSource _icon;
public ImageSource Icon
{
    get { return _icon; }
    set
    {
       _icon = value;
       RaisePropertyChanged();
   }
}
  1. Icon 从图库中获取图片后直接设置。
Icon = ImageSource.FromStream(() =>
{
    var imageStream = file.GetStream();
    return imageStream;
});

推荐阅读