我已经在控件的中央创建了一个渲染的选取器,使其居中居中,但是在进入页面时,它显示了以下错误:System.ArgumentException。
我附上代码以查看是否可以帮助我。
在项目中的新班级...
using Xamarin.Forms;
namespace MaterialFUR
{
public class PickerCentrat : Picker
{
}
}
在Android中...
using MaterialFUR;
using MaterialFUR.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(PickerCentrat), typeof(RendererPicker))]
namespace MaterialFUR.Droid
{
public class RendererPicker : EntryRenderer
{
public RendererPicker(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Gravity = GravityFlags.CenterHorizontal;
}
}
}
}
在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="MaterialFUR.AgafarMaterialPage"
xmlns:local="clr-namespace:MaterialFUR"
Title="Agafar Material">
<ContentPage.Content>
<StackLayout HorizontalOptions="Center" VerticalOptions="Center" Spacing="25">
<Button x:Name="btnScan" Clicked="btnScan_Clicked" BackgroundColor="Gray" TextColor="White" Text="Llegir codi de barres" FontSize="30" FontAttributes="Bold" BorderRadius="30"/>
<Entry x:Name="entCodi" TextColor="Black" FontSize="30" FontAttributes="Bold" Keyboard="Default" Placeholder="Codi" HorizontalTextAlignment="Center"/>
<Entry x:Name="entQtat" TextColor="Black" FontSize="30" FontAttributes="Bold" Keyboard="Numeric" Placeholder="Quantitat" HorizontalTextAlignment="Center"/>
<local:PickerCentrat x:Name="pckUbicacio" Title="Ubicació" TextColor="Black" FontSize="30" FontAttributes="Bold">
<Picker.Items>
<x:String>SATMOBIL-1</x:String>
<x:String>SATMOBIL-2</x:String>
<x:String>SATMOBIL-3</x:String>
<x:String>SATMOBIL-4</x:String>
<x:String>SATMOBIL-5</x:String>
<x:String>SATMOBIL-6</x:String>
<x:String>SATMOBIL-7</x:String>
<x:String>SATMOBIL-8</x:String>
<x:String>SATMOBIL-9</x:String>
<x:String>TALLER</x:String>
</Picker.Items>
</local:PickerCentrat>
<StackLayout Orientation="Horizontal" Margin="0,100">
<Button x:Name="btnConfirmar" Clicked="BtnConfirmar_Clicked" BackgroundColor="SteelBlue" TextColor="White" Text="Confirmar" FontSize="30" FontAttributes="Bold" BorderRadius="30"/>
<Button x:Name="btnCancelar" Clicked="BtnCancelar_Clicked" BackgroundColor="Gray" TextColor="White" Text="Cancelar" FontSize="30" FontAttributes="Bold" BorderRadius="30"/>
</StackLayout>
</StackLayout>
</ContentPage.Content>
感谢您的帮助,谢谢!
答案 0 :(得分:0)
您的课程需要继承自PickerRenderer
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(PickerCentrat), typeof(RendererPicker))]
namespace MaterialFUR.Droid
{
public class RendererPicker : PickerRenderer
{
public RendererPicker(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Gravity = GravityFlags.CenterHorizontal;
}
}
}
}
对于CustomRenderer需要继承哪个类有疑问时,您始终可以检查 Xamarin.Forms 源代码here(对于Android)。
希望这会有所帮助。