简体   繁体   English

以 xamarin 形式拍摄照片

[英]Capture photo in xamarin forms

I want to take multiple photos and it is shown in a list, but I must use the mvvm structure if I put this code in the CS of the XAML it works but I cannot use it that way ...我想拍摄多张照片并显示在列表中,但如果我将此代码放入 XAML 的 CS 中,我必须使用 mvvm 结构它可以工作,但我不能那样使用它......

The view model works and everything but does not show the image on the screen视图模型可以正常工作,但不会在屏幕上显示图像

What can be ??什么可以??

You can tell me that I am doing wrong he tried everything but he does not show me the image.你可以告诉我我做错了,他尝试了一切,但他没有向我展示图像。

Reference XAML (view) SEE MODEL and MODEL public class CapturePhotoViewModel : BaseViewModel { ObservableCollection Photos = new ObservableCollection();参考 XAML(视图)查看模型和模型公共类 CapturePhotoViewModel : BaseViewModel { ObservableCollection Photos = new ObservableCollection();

    public ICommand CapturePhotoCommand => new Command(async (s) => await CaptureFoto());
    private async Task CaptureFoto()
    {
        var IniciandoEvento = await CrossMedia.Current.Initialize();

        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsPickPhotoSupported || !CrossMedia.IsSupported || !IniciandoEvento)
        {
            await DialogService.DisplayAlertAsync("No se puede Acceder a la camara", "¡Error!", "Aceptar");
            return;
        }

        var newPhoto = Guid.NewGuid();
        using (var photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions()
        {
            Name = newPhoto.ToString(),
            SaveToAlbum = true,
            DefaultCamera = CameraDevice.Rear,
            Directory = "Demo",
            CustomPhotoSize = 50
        }))
        {
            if (string.IsNullOrWhiteSpace(photo?.Path))
            {
                return;
            }

            var newphotomedia = new MediaModel()
            {
                MediaID = newPhoto,
                Path = photo.Path,
                LocalDateTime = DateTime.Now
            };
            Photos = new ObservableCollection<MediaModel>();
            Photos.Add(newphotomedia);


        }


    }
}

THIS IS MY XAML CODE which goes the list of image that is captured, but I do not know what I am doing wrong and does not show me the image这是我的 XAML 代码,其中包含捕获的图像列表,但我不知道我做错了什么并且没有向我显示图像

 <Button Text="Tomar Foto" x:Name="photobutton" Command="{Binding CapturePhotoCommand}"/>
        <ListView x:Name="ListPhotos" ItemsSource="{Binding Photos.Source}" RowHeight="400"
            HeightRequest="300">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Frame  OutlineColor="LightGray" HasShadow="true" Margin="4" WidthRequest="200" HeightRequest="250">
                                <Frame.Content>
                                    <StackLayout>
                                        <Image Source="{Binding Source}" VerticalOptions="StartAndExpand" HeightRequest="280"/>
                                        <Button Text="Delete" />
                                    </StackLayout>

                                </Frame.Content>

                            </Frame>
                        </Grid>


                    </ViewCell>

                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Y Este es mi model Y Este es mi 模型

 public class MediaModel
{
    public Guid MediaID { get; set; }
    public string Path { get; set; }
    public DateTime LocalDateTime { get; set; }

    private FileImageSource source = null;
    public FileImageSource Source => source ?? (source = new FileImageSource() { File = Path });
}

first, get rid of this line首先,摆脱这条线

Photos = new ObservableCollection<MediaModel>();

then fix your ItemsSource binding然后修复您的ItemsSource绑定

<ListView x:Name="ListPhotos" ItemsSource="{Binding Photos}" RowHeight="400"

then fix your Image binding然后修复您的图像绑定

<Image Source="{Binding Path}" VerticalOptions="StartAndExpand" HeightRequest="280"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM