美好的一天。我尝试使用这个https://developer.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/,但我有一些问题。Xamarin AXML麻烦
我尝试将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(Main.axml在CameraApp.Droid)
<?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);
}
}
}
可以效仿的榜样像下面这样: https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Camera –
@JonDouglas感谢 – Evgeniy175
请包括被抛出的异常。 – matthewrdev