Xamarin Forms 中的自定义页面渲染器未扩充 Android 布局页面

标签 android xamarin xamarin.forms custom-renderer

我正在使用 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 的布局资源。运行构造函数是因为在控制台中,它打印出“页面已呈现”。但是在设备屏幕上,它只是显示这样的空白页面。

enter image description here

这是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>

实际上,它应该显示一个带有文本“This is the camera page”的 TextView。但是正如您在屏幕截图中看到的那样,没有显示任何内容。我的代码有什么问题,我该如何解决?

最佳答案

您忘记将View 添加到您的页面。

在膨胀之后,Inflate() 方法会返回一个View,您需要将这个View 添加到您的页面。我找到了一个 article对你来说,你需要在 OnElementChanged 方法中做一些事情,像这样:

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Page> e)
    {
        base.OnElementChanged(e);

        var activity = this.Context as Activity;
        view = LayoutInflater.From(this.Context).Inflate(Resource.Layout.CameraLayout,null, false); this, false);

        AddView(view);
    }

将您的 TextView 更改为:

  <TextView
            android:gravity="center" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="This is the camera page" 
            />

添加OnLayout方法:

    protected override void OnLayout(bool changed, int l, int t, int r, int b)
    {
        base.OnLayout(changed, l, t, r, b);
        var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
        var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);
        view.Measure(msw, msh);
        view.Layout(0, 0, r - l, b - t);
    }

关于Xamarin Forms 中的自定义页面渲染器未扩充 Android 布局页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49280855/

相关文章:

android - 我们如何区分闪存和SD卡内存?

android - 在方法中我的列表有元素,但是当我出来时列表是空的

ios - 插入 Xamarin NSMutableArray<T> 导致超出范围异常

visual-studio - Visual Studio 和 Xamarin : Unable to find packages

c# - 为什么找不到我的 ResourceDictionary xaml 文件?

android - 两个移动设备之间直接通信

android - 旋转后的 OpenGL ES 2 翻译

xamarin - 如何使我的应用程序与智能电视兼容?

android - 如果我使用 Xamarin 在 Android 设备上部署应用程序,它会损坏吗?

c# - 后面生成的代码抛出 System.ArgumentNullException