xamarin - 使用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 文件中。
问候
解决方案
Icon
从更改string
为ImageSource
。
private ImageSource _icon;
public ImageSource Icon
{
get { return _icon; }
set
{
_icon = value;
RaisePropertyChanged();
}
}
Icon
从图库中获取图片后直接设置。
Icon = ImageSource.FromStream(() =>
{
var imageStream = file.GetStream();
return imageStream;
});
推荐阅读
- google-sheets - 使用自动填充数组公式进行关键字搜索
- javascript - 更新 Vuex 状态时不显示 V-snackbar
- python - 美丽的汤玻璃门页
- python - 用户警告:从版本 2.2.1 开始,macOS 分发轮中的库文件由 Apple Clang (Xcode_8.3.3) 编译器构建
- python - NameError:名称“计数”未定义
- javascript - Javascript 如何从 CLI 发出噪音
- docker - 在 Docker 场景中将 `/var/run/docker.sock` 挂载到 Docker 中的结果是什么?
- java - 按钮不在我告诉他们的地方
- python - 带有 Django 的 Python API
- xamarin.forms - 来自 URL 的 Skiasharp 贷款图像并在 Xamarin 表单中的 Canvas 上绘制