【问题标题】:Android Layout page is not inflated for custom page renderer in Xamarin FormsXamarin Forms 中的自定义页面渲染器不会为 Android 布局页面充气
【发布时间】: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


    【解决方案1】:

    您忘记将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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多