如 @skalpel02所述,您需要在每个平台中在 picker class class class renderer s。在那里,您有能力与平台的本机API进行互动。
As mentioned by @Skalpel02, you need to sub-class the Picker class and implement the corresponding Renderers in each platform. There, you have the ability to interact with native APIs of the platform.
First,a custom Picker control can be created by subclassing the Picker control, as shown in the following code:
public class BorderlessPicker : Picker
{
public BorderlessPicker() : base()
{
}
}
Second:Create the Custom Renderer on each Platform,Override the OnElementChanged method and write logic to customize the control,then Add an ExportRenderer attribute to the custom renderer class to specify that it will be used.
In Android:
[assembly: ExportRenderer(typeof(BorderlessPicker), typeof(BordlessPickerRenderer))]
namespace AppPicker01.Droid
{
public class BordlessPickerRenderer : PickerRenderer
{
public BordlessPickerRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
Control.Background = null;
}
}
}
public partial class PickerCustomList : ContentPage
{
public int SelectedIndex = -1;
ObservableCollection<CustomItem> myItems= new ObservableCollection<CustomItem>();
public ObservableCollection<CustomItem> Items
{
get { return myItems; }
set {
myItems = value;
OnPropertyChanged();
}
}
public PickerCustomList()
{
InitializeComponent();
BindingContext = this;
}
private void Button_Clicked(object sender, EventArgs e)
{
SelectedIndex = -1;
App.Current.MainPage.Navigation.PopModalAsync();
MessagingCenter.Send<PickerCustomList>(this, "finish");
}
private void ViewCell_Tapped(object sender, EventArgs e)
{
SelectedIndex = Items.IndexOf(((CustomItem)((ViewCell)sender).BindingContext));
App.Current.MainPage.Navigation.PopModalAsync();
MessagingCenter.Send<PickerCustomList>(this, "finish");
}
}
创建“ pickerCustomList” 。 “ nofollow noreferrer”>
You can easily create your own control that doesn't need a renderer and works on iOS, Android, and UWP. Here my solution.
You have to create a View "PickerCustom" for the control
发布评论
评论(3)
如 @skalpel02所述,您需要在每个平台中在
picker
class class class renderer s。在那里,您有能力与平台的本机API进行互动。As mentioned by @Skalpel02, you need to sub-class the
Picker
class and implement the correspondingRenderer
s in each platform. There, you have the ability to interact with native APIs of the platform.这可以通过。
首先,可以通过子类别来创建自定义选择器控件,如以下代码所示:
second :在每个平台上创建自定义渲染器,覆盖
onelementChanged
方法并编写逻辑以自定义控件,然后将exportrenderer
属性添加到自定义渲染器类中,以指定将使用它。在Android中:
}
在iOS中:
最后但并非最不重要的一点是,在XAML中使用自定义选择器控制:
屏幕截图:
女士官方文档链接:
This could be implemented by custom renderer.
First,a custom Picker control can be created by subclassing the Picker control, as shown in the following code:
Second:Create the Custom Renderer on each Platform,Override the
OnElementChanged
method and write logic to customize the control,then Add anExportRenderer
attribute to the custom renderer class to specify that it will be used.In Android:
}
In iOS:
Last but not least, consume the custom picker control in Xaml:
Screenshot:
MS official docs link:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/
您可以轻松地创建不需要渲染器并在iOS,Android和UWP上使用的控件。这是我的解决方案。
和一个视图“ pickerCustomList”为“ pickerCustom”创建一个视图
您必须使用此代码在后面的该代码
为选择的视图“ pickerCustomList”
“ pickerCustom”,并在
创建“ pickerCustomList” 。 “ nofollow noreferrer”>
You can easily create your own control that doesn't need a renderer and works on iOS, Android, and UWP. Here my solution.
You have to create a View "PickerCustom" for the control
with this code behind
And a View "PickerCustomList" for the choice
with this code behind