2014-07-22 31 views
1

有誰在Xamarin.Forms.Labs'攝像功能的Xamarin.Forms的例子嗎?Xamarin形式 - Xamarin上頁構成實驗室相機顯示出來

我試圖讓它工作,但它似乎並沒有工作。

這裏是我的代碼:

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; 
    } 
} 

回答

0

這個工作對我來說:

在iOS的項目AppDelegate.cs:

公共覆蓋布爾FinishedLaunching(UIApplication的應用程序,NSDictionary的選項) { 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; 
     }); 
    } 
} 
+0

它仍然不起作用,iamge沒有出現,我得到的唯一一個操作可以一次處於活動狀態。 我的代碼在你的電腦上工作嗎? – user3841879

+0

修改後的代碼適用於我的電腦,不調用GetPicture() –

+0

的版本可以發佈您的整個代碼plz嗎? – user3841879