【发布时间】:2018-08-23 03:59:28
【问题描述】:
我正在使用 Xamarin.Forms 为 IOS 和 Android 平台开发一个移动应用程序。首先,我刚开始使用 Xamarins。我现在要做的是尝试为 iOS 和 Android 呈现不同的页面。我正在使用自定义页面渲染器。我调用了渲染的自定义页面,但问题是当我在 Android 中膨胀布局时,它似乎没有膨胀 axml 文件并且没有显示任何内容。
我在 App.xaml.cs 中像这样启动了我的自定义 CameraPage
MainPage = new NavigationPage(new CameraPage());
这是我的 CameraPage.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="MementoApp.Views.CameraPage">
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
这是我的 CameraPage.xaml.cs
namespace MementoApp.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CameraPage : ContentPage
{
public CameraPage ()
{
InitializeComponent ();
}
}
}
在 Android 项目中,我创建了一个名为 CameraPageRenderer.cs 的新类。这是该类的代码。
[assembly: ExportRenderer(typeof(CameraPage), typeof(CameraPageRenderer))]
namespace MementoApp.Droid
{
public class CameraPageRenderer : PageRenderer
{
global::Android.Views.View view;
Activity activity;
bool flashOn;
public CameraPageRenderer() : base(null)
{
throw new Exception("This constructor should never be used");
}
public CameraPageRenderer(Context context) : base(context)
{
this.activity = context as Activity;
LayoutInflater.From(this.Context).Inflate(Resource.Layout.CameraLayout,null, false);
Console.WriteLine("Page is rendered");
}
}
}
如您所见,在构造函数中,我正在扩展名为 CameraLayout 的布局资源。构造函数运行是因为在控制台中,它打印出“页面已呈现”。但在设备屏幕上,它只是显示这样的空白页面。
这是Android项目中的CameraLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" a:text="This is the camera page" xmlns:a="http://schemas.android.com/tools"/>
</LinearLayout>
实际上,它应该显示一个带有文本的 TextView,“这是相机页面”。但正如您在屏幕截图中看到的那样,没有显示任何内容。我的代码有什么问题,我该如何解决?
【问题讨论】:
标签: android xamarin xamarin.forms custom-renderer