1

有人在 Xamarin.Forms.Labs 的相机功能的 Xamarin.Forms 中有一个例子吗?

我试图让它工作,但它似乎根本不起作用。

这是我的代码:

public partial class CameraPictureInfoPage : ContentPage
{   
    public CameraPictureInfoPage ()
    {
        Image img = new Image ();
        this.Appearing += async (s, e) => {
            img.Source = await GetPicture (true);
        };


        this.Content = new StackLayout {
            Orientation = StackOrientation.Vertical,
            VerticalOptions = LayoutOptions.Center,
            WidthRequest = 250,
            Padding = 40, Spacing = 10,
            Children = {
                img
            }
        };
    }


    async Task<ImageSource> GetPicture(bool chooseNotTake){
        var mediaPicker = DependencyService.Get<IMediaPicker> (); 
        ImageSource imgSource  = null;
        if (mediaPicker != null) {
            Task<MediaFile> pick;
            if (chooseNotTake) {
                pick = mediaPicker.TakePhotoAsync (new CameraMediaStorageOptions {
                    DefaultCamera = CameraDevice.Rear, 
                    MaxPixelDimension = 1024,
                });
            } else {
                pick = mediaPicker.SelectPhotoAsync (new CameraMediaStorageOptions{ MaxPixelDimension = 1024 });
            } 

            await pick.ContinueWith (t => {
                if (!t.IsFaulted && !t.IsCanceled) {
                    var mediaFile = t.Result; 
                    MemoryStream mstr = new MemoryStream ();
                    mediaFile.Source.CopyTo (mstr);
                    imgSource = ImageSource.FromStream (() => mstr); 
                }
                return imgSource; 
            });
        }

        return imgSource;
    }
}
4

1 回答 1

0

这对我有用:

在 AppDelegate.cs 的 iOS 项目中:

public override bool FinishedLaunching (UIApplication app, NSDictionary options) { SetIoc ();

    Forms.Init ();

    window = new UIWindow (UIScreen.MainScreen.Bounds); 

    window.RootViewController = App.GetMainPage ().CreateViewController ();
    window.MakeKeyAndVisible ();

    return true;
}

private void SetIoc ()
{
    var resolverContainer = new SimpleContainer ();
    resolverContainer.Register<IDevice> (t => AppleDevice.CurrentDevice)
        .Register<IDisplay> (t => t.Resolve<IDevice> ().Display)
        .Register<IDependencyContainer> (t => resolverContainer);

    Resolver.SetResolver (resolverContainer.GetResolver ());
}

在表单项目中:

private async Task<ImageSource> GetPicture(bool chooseNotTake){
  var mediaPicker = DependencyService.Get<IMediaPicker> (); 
  ImageSource imgSource  = null;
  if (mediaPicker != null) {
      Task<MediaFile> pick;
      if (!chooseNotTake) {
        pick = mediaPicker.TakePhotoAsync (new CameraMediaStorageOptions {
          DefaultCamera = CameraDevice.Rear, 
          MaxPixelDimension = 1024,
        });
      } else {
          pick = mediaPicker.SelectPhotoAsync (new CameraMediaStorageOptions{ MaxPixelDimension = 1024 });
      } 

     await pick.ContinueWith (t => {
        if (!t.IsFaulted && !t.IsCanceled) {
           var mediaFile = t.Result; 
           MemoryStream mstr = new MemoryStream();
           mediaFile.Source.CopyTo(mstr);
           imgSource = ImageSource.FromStream (() => mstr); 
    }
    return imgSource; 
  }
}

请记住,您可能需要通过其他方式存储图像流,否则它可能会过早地被垃圾收集。


要让拍照者在启动时出现,您必须避免async并关注Appearing事件。在页面的构造函数中添加:

public class PhotoPage :  ContentPage
{
    Image img = new Image ();
    IMediaPicker picker = null; 
    Task<MediaFile> task = null;

    public PhotoPage ()
    {  
        img.WidthRequest = 60;
        img.HeightRequest = 60;
        img.BackgroundColor = Color.Silver;

        this.Content = new StackLayout {
            Orientation = StackOrientation.Vertical,
            VerticalOptions = LayoutOptions.Center,
            BackgroundColor = Color.Aqua,
            WidthRequest = 250,
            Padding = 40, Spacing = 10,
            Children = {
                img, 
            }
        }; 


        this.Appearing += (s, e) => { 
            picker = DependencyService.Get<IMediaPicker> ();  
            task = picker.SelectPhotoAsync (new CameraMediaStorageOptions{ MaxPixelDimension = 1024 });  
        }; 

        Device.StartTimer (TimeSpan.FromMilliseconds (250), () => {
            if (task != null) {
                if (task.Status == TaskStatus.RanToCompletion) {
                    Device.BeginInvokeOnMainThread (() => {
                        img.Source = ImageSource.FromStream (() => task.Result.Source);
                    });  
                }

                return  task.Status != TaskStatus.Canceled
                    && task.Status != TaskStatus.Faulted
                    && task.Status != TaskStatus.RanToCompletion;
            }
            return true;
        }); 
    }
}
于 2014-07-22T20:51:01.220 回答