元素未在 Xamarin Forms Android 自定义渲染器中显示
Element not displaying in Xamarin Forms Android custom renderer
使用 Xamarin Forms 自定义渲染器,我需要在实时相机视图(视频)上叠加十字准线图像。我让 iOS 渲染器工作,它用于 BoxView。 Android 然而对我来说是相当新的。所以在 Android BoxView 渲染器中,我从一些简单的东西开始,只是尝试为初学者显示一个 ImageView 或一个 EditText 控件。
尽管进行了大量谷歌搜索,但仍在努力解决这个问题。
这是我的代码。它执行,但没有控件(图像)出现在屏幕上。
共享代码:
using Xamarin.Forms;
namespace MyApp.Views
{
public class CameraBoxView : BoxView
// Uses custom renderer CameraBoxViewRenderer.cs
{}
}
ANDROID 渲染器:
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 );
}
}
}
添加以下代码解决了问题。感谢 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 );
}
使用 Xamarin Forms 自定义渲染器,我需要在实时相机视图(视频)上叠加十字准线图像。我让 iOS 渲染器工作,它用于 BoxView。 Android 然而对我来说是相当新的。所以在 Android BoxView 渲染器中,我从一些简单的东西开始,只是尝试为初学者显示一个 ImageView 或一个 EditText 控件。
尽管进行了大量谷歌搜索,但仍在努力解决这个问题。
这是我的代码。它执行,但没有控件(图像)出现在屏幕上。
共享代码:
using Xamarin.Forms;
namespace MyApp.Views
{
public class CameraBoxView : BoxView
// Uses custom renderer CameraBoxViewRenderer.cs
{}
}
ANDROID 渲染器:
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 );
}
}
}
添加以下代码解决了问题。感谢 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 );
}