码迷,mamicode.com
首页 > 其他好文 > 详细

xamarin.form 添加拍照功能

时间:2018-12-01 11:10:30      阅读:706      评论:0      收藏:0      [点我收藏+]

标签:sha   访问   extern   ble   ram   mina   安装   glob   imp   

  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 添加拍照功能

标签:sha   访问   extern   ble   ram   mina   安装   glob   imp   

原文地址:https://www.cnblogs.com/jiecaoge/p/10048059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!