xamarin.form 添加拍照功能
Posted jiecaoge
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xamarin.form 添加拍照功能相关的知识,希望对你有一定的参考价值。
1。使用nuget安装安装XLabs.Serialization.JSON,和XLabs.Forms
2。在android端增加如下代码
[Activity(Label = "App2", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : XFormsApplicationDroid { protected override void OnCreate(Bundle savedInstanceState) { //TabLayoutResource = Resource.Layout.Tabbar; //ToolbarResource = Resource.Layout.Toolbar; SetIoc(); base.OnCreate(savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); } private void SetIoc() { var resolverContainer = new global::XLabs.Ioc.SimpleContainer(); resolverContainer.Register<IMediaPicker, MediaPicker>(); XLabs.Ioc.Resolver.SetResolver(resolverContainer.GetResolver()); }
在ios端增加如下代码
// The UIApplicationDelegate for the application. This class is responsible for launching the // User Interface of the application, as well as listening (and optionally responding) to // application events from iOS. [Register("AppDelegate")] public partial class AppDelegate : XLabs.Forms.XFormsApplicationDelegate { // // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching(UIApplication app, NSDictionary options) { SetIoc(); global::Xamarin.Forms.Forms.Init(); LoadApplication(new App()); return base.FinishedLaunching(app, options); } private void SetIoc() { var resolverContainer = new global::XLabs.Ioc.SimpleContainer(); resolverContainer.Register<IMediaPicker, MediaPicker>(); XLabs.Ioc.Resolver.SetResolver(resolverContainer.GetResolver()); } }
在ios端,需要编辑info.plist,增加相机使用权限
<key>NSCameraUsageDescription</key> <string>App需要您的同意,才能访问相机</string>
在android端需要右键-》属性-》Android清单-》所需权限中勾选CAMER和WRITE_EXTERNAL_STORAGE
如果你需要选取照片功能
private async void Button_Clicked_1(object sender, EventArgs e) { IMediaPicker mediaPicker = Resolver.Resolve<IMediaPicker>(); var mediaFile = await mediaPicker.SelectPhotoAsync(new CameraMediaStorageOptions { DefaultCamera = CameraDevice.Front, }); Photo.Source = ImageSource.FromFile(mediaFile.Path); }
以上是关于xamarin.form 添加拍照功能的主要内容,如果未能解决你的问题,请参考以下文章
如何在Xamarin.Form中的Frame Renders中使3个圆角变圆?