【问题标题】:Xamarin AXML troublesXamarin AXML 问题
【发布时间】:2016-04-13 03:09:57
【问题描述】:

美好的一天。我尝试使用这个https://developer.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/,但我遇到了一些问题。

我尝试将 XAML(以下)实现为 AXML(以下)。但是我对 CameraApp.Droid.MainActivity 的 OnCreate() 方法的实现有例外。

请帮助我了解 Android 应用程序中的 AXML 用法,因为我不明白它应该如何实现。

Project looks like this

我需要在 AXML 中实现如下代码(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:cameraApp="clr-namespace:CameraApp;assembly=CameraApp.Droid"
             x:Class="CameraApp.CameraPage">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="*"></RowDefinition>
      <RowDefinition Height="100"></RowDefinition>
    </Grid.RowDefinitions>
    <cameraApp:CameraButton Grid.Row="0"/>
    <ScrollView Grid.Row="1">
      <Label Text="gallery" />
    </ScrollView>
  </Grid>
</ContentPage>

我尝试将其重写为 AXML(CameraApp.Droid 中的 Main.axml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <CameraButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gallery"
        android:layout_weight="2" />
</LinearLayout>

CameraApp.App.cs:

using Xamarin.Forms;

namespace CameraApp
{
    public class App : Application
    {
        public App ()
        {
            MainPage = new CameraPage();
        }
    }
}

CameraApp.CameraButton.cs:

using Xamarin.Forms;

namespace CameraApp
{
    public class CameraButton : Button { }
}

CameraButtonRenderer.cs:

using CameraApp;
using CameraApp.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(CameraButton), typeof(CameraButtonRenderer))]
namespace CameraApp.Droid
{
    public class CameraButtonRenderer : ButtonRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                var nativeButton = Control;
                nativeButton.Click += delegate
                {
                    SetBackgroundColor(Android.Graphics.Color.LightGreen);
                };
                nativeButton.LongClick += delegate
                {
                    SetBackgroundColor(Android.Graphics.Color.Red);
                };
            }
        }
    }
}

CameraApp.Droid.MainActivity.cs:

using Android.App;
using Android.Content.PM;
using Android.OS;

namespace CameraApp.Droid
{
    [Activity (Label = "CameraApp", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
        }
    }
}

【问题讨论】:

标签: c# android xamarin xamarin.android


【解决方案1】:

Xamarin Media plugin 可让您直接从您的 Forms 项目访问相机,而无需编写自定义平台渲染器。

【讨论】:

  • 谢谢,它有效!但是如果我在 XAML 中有按钮,可以直接拍照到指定文件夹,而不用打开内置相机?
猜你喜欢
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多