Xamarin.forms 如何在Xamarin表单中使用、转换或创建第三方android/iOS自定义控件?
最近,我对为android和iOS创建自定义控件很感兴趣,但也希望它们能够在Xamarin表单中使用。这有多可能?表单在幕后做了一些非常奇怪的事情吗?它是如何在堆栈布局或网格中调整大小的 作为一个非常简单的例子,Pluralsighti上的一个自定义android控件教程创建了一个“lengthpicker”控件。它由2个按钮和一个文本视图组成 Resources\layout\length\u picker.axmlXamarin.forms 如何在Xamarin表单中使用、转换或创建第三方android/iOS自定义控件?,xamarin.forms,custom-controls,custom-renderer,Xamarin.forms,Custom Controls,Custom Renderer,最近,我对为android和iOS创建自定义控件很感兴趣,但也希望它们能够在Xamarin表单中使用。这有多可能?表单在幕后做了一些非常奇怪的事情吗?它是如何在堆栈布局或网格中调整大小的 作为一个非常简单的例子,Pluralsighti上的一个自定义android控件教程创建了一个“lengthpicker”控件。它由2个按钮和一个文本视图组成 Resources\layout\length\u picker.axml <?xml version="1.0" encoding="utf-8
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/minus_button"
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textAppearance="@android:style/TextAppearance.Large" />
<Button
android:id="@+id/plus_button"
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</merge>
在android项目中,我有一个CustomRenderer,它扩展了
LengthPickerRenderer.cs
public class LengthPicker : LinearLayout
{
private View mPlusButton;
private TextView mTextView;
private View mMinusButton;
private int mNumInches = 0;
public LengthPicker(Context context) :base(context)
{
Init();
}
public LengthPicker(Context context, IAttributeSet attrs): base(context, attrs)
{
Init();
}
private void Init()
{
LayoutInflater inflater = LayoutInflater.From(Context);
inflater.Inflate(Resource.Layout.length_picker,this);
mPlusButton = FindViewById<Button>(Resource.Id.plus_button);
mTextView = FindViewById<TextView>(Resource.Id.text);
mMinusButton = FindViewById<Button>(Resource.Id.minus_button);
UpdateControls();
ApplyButtonClickHandlers();
Orientation = Orientation.Horizontal;
this.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
}
private void ApplyButtonClickHandlers()
{
mPlusButton.Click += MPlusButton_Click;
mMinusButton.Click += MMinusButton_Click;
}
private void UpdateControls()
{
int feet = mNumInches / 12;
int inches = mNumInches % 12;
string text = $"{feet}' {inches}\"";
if(feet == 0)
{
text = $"{inches}\"";
}
else
{
if(inches == 0)
{
text = $"{feet}'";
}
}
mTextView.Text = text;
mMinusButton.Enabled = mNumInches > 0;
}
private void MMinusButton_Click(object sender, EventArgs e)
{
mNumInches--;
UpdateControls();
}
private void MPlusButton_Click(object sender, EventArgs e)
{
mNumInches++;
UpdateControls();
}
}
public class LengthPicker : Xamarin.Forms.View
{
public static readonly BindableProperty MinusColorProperty = BindableProperty.Create("MinusColor", typeof(Color), typeof(LengthPicker), Color.Default);
public Color MinusColor
{
get { return (Color)GetValue(MinusColorProperty); }
set { SetValue(MinusColorProperty, value); }
}
}
using ALengthPicker = MobileControls.Samples.LengthPicker; // LengthPicker in separater Android class library
using AButton = Android.Widget.Button;
using ATextView = Android.Widget.TextView;
namespace FormsMobileControlApp.Droid.Renderers
{
public class LengthPickerRenderer :ViewRenderer<LengthPicker,ALengthPicker>
{
private AButton _minusButton;
private AButton _plusButton;
private ATextView _text;
protected override void OnElementChanged(ElementChangedEventArgs<Controls.LengthPicker> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
if (Control == null)
SetNativeControl(new ALengthPicker(Forms.Context));
_minusButton = Control.FindViewById<AButton>(MobileControls.Resource.Id.minus_button);
_text = Control.FindViewById<ATextView>(MobileControls.Resource.Id.text);
_plusButton = Control.FindViewById<AButton>(MobileControls.Resource.Id.plus_button);
}
. . .
}
}
}
使用ALengthPicker=MobileControls.Samples.LengthPicker;//分离器Android类库中的长度选择器
使用AButton=Android.Widget.Button;
使用ATextView=Android.Widget.TextView;
命名空间FormsMobileControlApp.Droid.Renderers
{
公共类长度PickerRenderer:ViewRenderer
{
私人接驳按钮;
私人AButton_plusButton;
私有ATextView_文本;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement==null)
{
if(Control==null)
SetNativeControl(新的ALengthPicker(Forms.Context));
_minusButton=Control.FindViewById关于android上的自定义xamarin表单控件,但它不处理膨胀布局,而是覆盖draw方法
有人有这样做的经验吗