【问题标题】:How to load the native camera when a user opens a xamarin forms app当用户打开 xamarin 表单应用程序时如何加载本机相机
【发布时间】:2017-08-03 20:08:18
【问题描述】:

您好,我正在开发一个应用程序,该应用程序允许用户为笔记拍照,然后将它们发送给朋友。我正在使用 xamarin 表单构建此应用程序,并且我还使用媒体插件来访问本机相机,但用户必须按下按钮才能打开本机相机,所以我的问题是如何让相机尽快加载用户打开的应用程序?

这是我的 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" x:Class="SnapNote.CameraPage"
xmlns:local="clr-namespace:AppName;"
    BackgroundColor="{x:Static local:Colors.BackgroundColor}">
    <ContentPage.Padding>
        <OnPlatform
            x:TypeArguments="Thickness"
            iOS="10,20,10,10"
            Android="10,10,10,10" />
    </ContentPage.Padding>
    <StackLayout>

        <Image Source="TakePhotoButton.png">
    <Image.GestureRecognizers>
        <TapGestureRecognizer
                Tapped="Handle_Clicked"  />
  </Image.GestureRecognizers>
</Image>

        <Image x:Name="image"/>


        <Image Source="SendNoteButton.png">

</Image>


    </StackLayout>
</ContentPage> 

下面是代码:

using System;
using System.Collections.Generic;
using Plugin.Media;
using Plugin.Media.Abstractions;
using Xamarin.Forms;

namespace AppName
{
    public partial class CameraPage : ContentPage
    {
        public CameraPage()
        {
            InitializeComponent();
        }

        async void Handle_Clicked(object sender, System.EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
            {
                Directory = "MyPhoto",
                Name = "Nextflow.jpg",
                SaveToAlbum = true
            });

            if (file == null)
                return;

            image.Source = ImageSource.FromStream(() =>
            {
                var stream = file.GetStream();
                file.Dispose();
                return stream;
            });
        }
    }
}

任何帮助都会很棒!

提前致谢!

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    只需将您的相机逻辑移动到页面的 OnAppearing 事件中

          public override void OnAppearing() {
            await CrossMedia.Current.Initialize();
    
            var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
            {
                Directory = "MyPhoto",
                Name = "Nextflow.jpg",
                SaveToAlbum = true
            });
    
            if (file == null)
                return;
    
            image.Source = ImageSource.FromStream(() =>
            {
                var stream = file.GetStream();
                file.Dispose();
                return stream;
            });
          }
    

    【讨论】:

    • 感谢您的回复,当我尝试您的代码建议时,我收到此错误:CameraPage.xaml.cs(30,30): Error CS0507: AppName.CameraPage.OnAppearing()': cannot change access modifiers when overriding protected' 继承成员 `Xamarin.Forms. Page.OnAppearing()' (CS0507) (AppName) 还有其他建议吗?在此先感谢:)
    • 嗨,Jason,您对我给予赏金的以下问题有任何想法(+100)stackoverflow.com/questions/42728757/…
    • 请不要用无关的cmets劫持别人的问题
    猜你喜欢
    • 2010-11-28
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    相关资源
    最近更新 更多