none
微软的Xamarin.Essentials一个例程没看懂 RRS feed

  • Question

  • 微软的这个例程没看懂,

    这句话Console.WriteLine($"CapturePhotoAsync COMPLETED: {PhotoPath}");中

    没看到程序在哪里给PhotoPath赋值啊?

    Xamarin.Essentials:媒体选取器 - Xamarin | Microsoft Docs

    async Task TakePhotoAsync() { try { var photo = await MediaPicker.CapturePhotoAsync(); await LoadPhotoAsync(photo); Console.WriteLine($"CapturePhotoAsync COMPLETED: {PhotoPath}"); } catch (FeatureNotSupportedException fnsEx) { // Feature is now supported on the device } catch (PermissionException pEx) { // Permissions not granted } catch (Exception ex) { Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}"); } } async Task LoadPhotoAsync(FileResult photo) { // canceled if (photo == null) { PhotoPath = null; return; } // save the file into local storage var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName); using (var stream = await photo.OpenReadAsync()) using (var newStream = File.OpenWrite(newFile)) await stream.CopyToAsync(newStream); PhotoPath = newFile; }

    Wednesday, 23 June 2021 8:30 AM

Answers

  • 你好,

    代码中的PhotoPath指代的是控件Image的Source,

    <Image Source="{Binding PhotoPath}"/>

    如下定义它:

    string photoPath;
    public string PhotoPath
    {
           get => photoPath;
           set
           {
               if (photoPath == value)
                   return;
               photoPath = value;
               OnPropertyChanged();
           }
    }

    我们可以发现PhotoPath在LoadPhotoAsync方法中已经被赋值了:

    PhotoPath = newFile;

    顺便说下,有关Xamarin的问题,我建议你可以在下面的论坛提问。

    dotnet-xamarinforms

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Thursday, 24 June 2021 7:02 AM
    Moderator