使用Xamarin Forms+XLabs 完成 Camara access

Posted _iorilan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Xamarin Forms+XLabs 完成 Camara access相关的知识,希望对你有一定的参考价值。


1. 安装nuget
Xamarin.Forms
XLabs.Forms


2. MainActivity.cs  (android)


public class MainActivity : XFormsApplicationDroid
    {
        /// <summary>
        /// Called when [create].
        /// </summary>
        /// <param name="bundle">The bundle.</param>
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            if (!Resolver.IsSet)
            {
                this.SetIoc();
            }
            else
            {
                var app = Resolver.Resolve<IXFormsApp>() as IXFormsApp<XFormsApplicationDroid>;
                if (app != null) app.AppContext = this;
            }


            Xamarin.Forms.Forms.Init(this, bundle);
            this.LoadApplication(new App());
        }


        /// <summary>
        /// Sets the IoC.
        /// </summary>
        private void SetIoc()
        {
            var resolverContainer = new SimpleContainer();


            var app = new XFormsAppDroid();
            app.Init(this);


            resolverContainer.Register<IDevice>(t => AndroidDevice.CurrentDevice)
                .Register<IMediaPicker, MediaPicker>();


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




3. ViewModel (Portable)


CamaraViewModel.cs


从这里拿的
https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs




4. View (portable)


Photo.xaml


<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewModels="clr-namespace:PhotoSample.ViewModels;assembly=PhotoSample"
             x:Class="PhotoSample.Views.Photo">


  <ContentPage.BindingContext>
    <viewModels:CameraViewModel />
  </ContentPage.BindingContext>


  <StackLayout>
    <Button Text="Take Picture" Command="{Binding TakePictureCommand}" />
    <Button Text="Select Image from Picture Library" Command="{Binding SelectPictureCommand}" />
    <Button Text="Select Video from Picture Library " Command="{Binding SelectVideoCommand}" />
    <Image Source="{Binding ImageSource}" VerticalOptions="CenterAndExpand" />
    <Entry Text="{Binding VideoInfo}" VerticalOptions="CenterAndExpand" />
  </StackLayout>


</ContentPage>




在App.cs (portable) 初始view指向photo即可


MainPage = new Photo();


以上是关于使用Xamarin Forms+XLabs 完成 Camara access的主要内容,如果未能解决你的问题,请参考以下文章

xamarin.form 添加拍照功能

Xamarin.Forms - 打开对话框以搜索文件的最佳方式

如何将占位符颜色属性添加到 xamarin 表单中的自动完成字段?

Xamarin Forms Picker - iOS 上的“完成”文本

如何在 Xamarin Forms 中使用 java 代码来完成特定的工作

我们如何处理 Xamarin Forms Picker 的完成按钮单击事件?