使用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();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值