【问题标题】:Element not displaying in Xamarin Forms Android custom renderer元素未在 Xamarin Forms Android 自定义渲染器中显示
【发布时间】:2021-06-18 07:38:12
【问题描述】:

使用 Xamarin Forms 自定义渲染器,我需要在实时摄像头视图(视频)上叠加一个十字准线图像。我让 iOS 渲染器正常工作,它是用于 BoxView 的。然而,Android 对我来说相当新。所以在 Android BoxView 渲染器中,我从一些简单的东西开始,只是尝试为初学者显示一个 ImageView 或一个 EditText 控件。

尽管进行了大量的谷歌搜索,但仍在努力解决这个问题。

这是我的代码。它执行,但屏幕上没有显示控件(图像)。

共享代码:

using Xamarin.Forms;

namespace MyApp.Views
{
    public class CameraBoxView : BoxView
    // Uses custom renderer CameraBoxViewRenderer.cs
    {}
}

安卓渲染器:

using System;

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

using MyApp.Views;
using MyApp.Droid.Camera;

using Android.Content;
using Android.Widget;
using Android.Views;


[assembly:ExportRenderer (typeof(CameraBoxView), typeof(CameraBoxViewRenderer))]

namespace MyApp.Droid.Camera
{
    public class CameraBoxViewRenderer : BoxRenderer
    {
        private Context context;
        private ImageView imageView;
        private EditText editText;

        public CameraBoxViewRenderer ( Context context ) : base ( context )
        {
            this.context = context;
        }

        protected override void OnElementChanged ( ElementChangedEventArgs<BoxView> e )
        {
            base.OnElementChanged ( e );

            if ( e.OldElement != null || Element == null)
                return;

            CameraBoxView box = Element as CameraBoxView;

            imageView = new ImageView ( context );
            imageView.ContentDescription = "my image";
            imageView.SetImageResource ( Resource.Drawable.myimage );
            imageView.LayoutParameters = new LinearLayout.LayoutParams ( ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.MatchParent );
            imageView.SetScaleType ( ImageView.ScaleType.FitCenter );
            imageView.Visibility = ViewStates.Visible;
            this.AddView ( imageView );

            //editText = new EditText ( context );
            //editText.ContentDescription = "edit_text";
            //editText.LayoutParameters = new LinearLayout.LayoutParams ( ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.MatchParent );
            //editText.Visibility = ViewStates.Visible;
            //editText.Hint = "Hello world!";
            //this.AddView ( editText );
        }
    }
}

【问题讨论】:

  • 你可以为你想要的效果显示一个截图吗?
  • @Leo Zhu,我只希望全屏充满图像。或者,使用“编辑文本”查看带有提示的 EditText 框。目前屏幕仅显示为背景颜色。

标签: android xamarin.forms custom-renderer


【解决方案1】:

添加以下代码解决了这个问题。感谢Adam Kemp

        protected override void OnLayout ( bool changed, int l, int t, int r, int b )
        {
            base.OnLayout ( changed, l, t, r, b );

            GetChildAt ( 0 ).Layout ( 0, 0, r-l, b-t );
        }

        protected override void OnMeasure ( int widthMeasureSpec, int heightMeasureSpec )
        {
            base.OnMeasure ( widthMeasureSpec, heightMeasureSpec );

            GetChildAt (0).Measure ( widthMeasureSpec, heightMeasureSpec );
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 2018-07-29
    • 2020-01-14
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多