Xamarin Picker 查看无效选择 iOS 12
Xamarin Picker View invalid selection iOS 12
下面是 Xamarin 文档中已经共享的示例代码。
<Picker>
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Baboon</x:String>
<x:String>Capuchin Monkey</x:String>
<x:String>Blue Monkey</x:String>
<x:String>Squirrel Monkey</x:String>
<x:String>Golden Lion Tamarin</x:String>
<x:String>Howler Monkey</x:String>
<x:String>Japanese Macaque</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
当 运行 此代码在 iOS 12 模拟器中时,它可以工作,但是当您点击完成按钮时,selected 项目被更改并分配给列表中的第 4 项selected 项目。
例如,如果您 select 上面列表中的狒狒并点击完成按钮,select编辑的项目将更改为金狮面狨。
我不确定这个问题是否只与 iOS 12 有关。我正在下载以前版本的模拟器看看
我已经上传了模拟器的录像here,如果是OS相关问题或其他问题,谁能指导一下?
谢谢,
解决方法:如果还是不能解决问题,可以使用自定义渲染器,重写按钮的事件Done
.
in iOS project
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using UIKit;
using Foundation;
using ObjCRuntime;
using CoreGraphics;
[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
namespace xxx.iOS
{
public class MyPickerRenderer:PickerRenderer,IUIPickerViewDelegate,IUIPickerViewDataSource
{
string SelectedValue;
public MyPickerRenderer()
{
}
public nint GetComponentCount(UIPickerView pickerView)
{
return 1;
}
public nint GetRowsInComponent(UIPickerView pickerView, nint component)
{
return Element.Items.Count;
}
[Export("pickerView:titleForRow:forComponent:")]
public string GetTitle(UIPickerView pickerView, nint row, nint component)
{
return Element.Items[(int)row].ToString();
}
[Export("pickerView:didSelectRow:inComponent:")]
public void Selected(UIPickerView pickerView, nint row, nint component)
{
Control.Text = Element.Items[(int)row];
SelectedValue= Element.Items[(int)row];
}
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
SelectedValue = Element.Items[0];
UIPickerView pickerView = (UIPickerView)Control.InputView;
pickerView.WeakDelegate = this;
pickerView.DataSource = this;
UIToolbar toolbar = (UIToolbar)Control.InputAccessoryView;
UIBarButtonItem done = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done, (object sender, EventArgs click) =>
{
Control.Text = SelectedValue;
toolbar.RemoveFromSuperview();
pickerView.RemoveFromSuperview();
Control.ResignFirstResponder();
});
toolbar.Items = new UIBarButtonItem[] { done };
}
}
}
}
in forms
using System;
using Xamarin.Forms;
namespace app1
{
public class MyPicker:Picker
{
public MyPicker()
{
}
}
}
in xaml
<local:MyPicker>
<local:MyPicker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Baboon</x:String>
<x:String>Capuchin Monkey</x:String>
<x:String>Blue Monkey</x:String>
<x:String>Squirrel Monkey</x:String>
<x:String>Golden Lion Tamarin</x:String>
<x:String>Howler Monkey</x:String>
<x:String>Japanese Macaque</x:String>
</x:Array>
</local:MyPicker.ItemsSource>
</local:MyPicker>
Note: Here is a similar issue that you can refer
提到的解决方案here(清除模拟器内容)有效。
问题出在 Xcode 模拟器上,不确定是什么导致了这个问题。
下面是 Xamarin 文档中已经共享的示例代码。
<Picker>
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Baboon</x:String>
<x:String>Capuchin Monkey</x:String>
<x:String>Blue Monkey</x:String>
<x:String>Squirrel Monkey</x:String>
<x:String>Golden Lion Tamarin</x:String>
<x:String>Howler Monkey</x:String>
<x:String>Japanese Macaque</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
当 运行 此代码在 iOS 12 模拟器中时,它可以工作,但是当您点击完成按钮时,selected 项目被更改并分配给列表中的第 4 项selected 项目。
例如,如果您 select 上面列表中的狒狒并点击完成按钮,select编辑的项目将更改为金狮面狨。
我不确定这个问题是否只与 iOS 12 有关。我正在下载以前版本的模拟器看看
我已经上传了模拟器的录像here,如果是OS相关问题或其他问题,谁能指导一下?
谢谢,
解决方法:如果还是不能解决问题,可以使用自定义渲染器,重写按钮的事件Done
.
in iOS project
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using UIKit;
using Foundation;
using ObjCRuntime;
using CoreGraphics;
[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
namespace xxx.iOS
{
public class MyPickerRenderer:PickerRenderer,IUIPickerViewDelegate,IUIPickerViewDataSource
{
string SelectedValue;
public MyPickerRenderer()
{
}
public nint GetComponentCount(UIPickerView pickerView)
{
return 1;
}
public nint GetRowsInComponent(UIPickerView pickerView, nint component)
{
return Element.Items.Count;
}
[Export("pickerView:titleForRow:forComponent:")]
public string GetTitle(UIPickerView pickerView, nint row, nint component)
{
return Element.Items[(int)row].ToString();
}
[Export("pickerView:didSelectRow:inComponent:")]
public void Selected(UIPickerView pickerView, nint row, nint component)
{
Control.Text = Element.Items[(int)row];
SelectedValue= Element.Items[(int)row];
}
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
SelectedValue = Element.Items[0];
UIPickerView pickerView = (UIPickerView)Control.InputView;
pickerView.WeakDelegate = this;
pickerView.DataSource = this;
UIToolbar toolbar = (UIToolbar)Control.InputAccessoryView;
UIBarButtonItem done = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done, (object sender, EventArgs click) =>
{
Control.Text = SelectedValue;
toolbar.RemoveFromSuperview();
pickerView.RemoveFromSuperview();
Control.ResignFirstResponder();
});
toolbar.Items = new UIBarButtonItem[] { done };
}
}
}
}
in forms
using System;
using Xamarin.Forms;
namespace app1
{
public class MyPicker:Picker
{
public MyPicker()
{
}
}
}
in xaml
<local:MyPicker>
<local:MyPicker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Baboon</x:String>
<x:String>Capuchin Monkey</x:String>
<x:String>Blue Monkey</x:String>
<x:String>Squirrel Monkey</x:String>
<x:String>Golden Lion Tamarin</x:String>
<x:String>Howler Monkey</x:String>
<x:String>Japanese Macaque</x:String>
</x:Array>
</local:MyPicker.ItemsSource>
</local:MyPicker>
Note: Here is a similar issue that you can refer
提到的解决方案here(清除模拟器内容)有效。
问题出在 Xcode 模拟器上,不确定是什么导致了这个问题。