【发布时间】: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 用法,因为我不明白它应该如何实现。
我需要在 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);
}
}
}
【问题讨论】:
-
您可以按照以下示例进行操作:github.com/XLabs/Xamarin-Forms-Labs/wiki/Camera
-
@JonDouglas 谢谢
-
请包括被抛出的异常。
标签: c# android xamarin xamarin.android