Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MVVM上的Xaml Xamarin窗体上显示图像_Xaml_Xamarin_Mvvm_Xamarin.forms_Prism - Fatal编程技术网

如何在MVVM上的Xaml Xamarin窗体上显示图像

如何在MVVM上的Xaml Xamarin窗体上显示图像,xaml,xamarin,mvvm,xamarin.forms,prism,Xaml,Xamarin,Mvvm,Xamarin.forms,Prism,我曾尝试使用媒体插件显示和显示手机摄像头中的图像。但在选择图像后,它不会显示。我正在使用MVVM。 我非常感谢你的帮助 private byte[] imgAsBytes; private ImageSource imageSource; //constructor public NoticesAdminViewModel() { _connection = DependencyServ

我曾尝试使用媒体插件显示和显示手机摄像头中的图像。但在选择图像后,它不会显示。我正在使用MVVM。 我非常感谢你的帮助

        private byte[] imgAsBytes;
        private ImageSource imageSource;

        //constructor

        public NoticesAdminViewModel()
         {
            _connection = DependencyService.Get<IConnection>().GetConnection();
            UploadImage = new DelegateCommand(async () => await GetImage());
         }

        public DelegateCommand UploadImage { get; set; }

        public ImageSource DisplayImageSource
        {
            get { return imageSource; }
            set
            {
                imageSource = value;
                SetProperty(ref imageSource, value);
            }
        }

        public async Task GetImage()
         {
            file = await DependencyService.Get<IMediaFile>().GetFile();

           //file is not null
            DisplayImageSource = ImageSource.FromStream(() => file.GetStream());

         }

        //My Xaml

        //Display Image Here
         <Image 
            Aspect="AspectFit"
            HeightRequest="200"
            Source="{Binding DisplayImageSource}"
            WidthRequest="200"/>

         // Button to call image

          <Image Source="camera.png" HorizontalOptions="Center">
             <Image.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding UploadImage}" />
             </Image.GestureRecognizers>
          </Image>
private byte[]imgAsBytes;
私有镜像源镜像源;
//建造师
公共公告管理视图模型()
{
_connection=DependencyService.Get().GetConnection();
UploadImage=newdelegateCommand(异步()=>await-GetImage());
}
公共DelegateCommand上载映像{get;set;}
公共图像源显示图像源
{
获取{return imageSource;}
设置
{
imageSource=值;
SetProperty(参考图像源,值);
}
}
公共异步任务GetImage()
{
file=wait DependencyService.Get().GetFile();
//文件不为空
DisplayImageSource=ImageSource.FromStream(()=>file.GetStream());
}
//我的Xaml
//在此显示图像
//按钮调用图像
哪个文件成功返回MediaFile内容,我可以将图像上载到服务器,但显示图像的Xaml为空

我做错了什么?
谢谢

首先,您的代码显示您正在将映像的源绑定到UploadImage,但您希望将其绑定到ImageSource属性

另外,在GetImage()方法中,您将文件内容复制到内存流中,并将其复制到字节数组中,但从未使用过它。为什么?

然后立即处理文件和流,并设置指向file.GetStream()的图像源,但您已经处理了该文件

试着把这些都删掉,然后做:

public async Task<ImageSource> GetImage()
 {
    file = await DependencyService.Get<IMediaFile>().GetFile();

    ImageSource = ImageSource.FromStream(() => file.GetStream());

    return ImageSource;
 }
public异步任务GetImage()
{
file=wait DependencyService.Get().GetFile();
ImageSource=ImageSource.FromStream(()=>file.GetStream());
返回图像源;
}

AFAIK您无法将ImageSource绑定到命令:
Source=“{Binding UploadImage}”
Hi Ben,谢谢您的回复。我正在使用内存流将图像文件上传到远程服务器,它工作正常。我试着回答,但屏幕上没有显示图像。仍在挣扎。您确定OnPropertyChanged事件正在触发吗。我问,因为我刚刚注意到您在调用SetProperty之前正在设置imageSource字段。SetProperty方法可能仅在后台字段已更改时触发事件,但如果在调用它之前设置它,则不会有什么不同。您可能会删除imageSource=value;