C# PickerView工具栏的“完成”按钮不工作
“完成”按钮添加到pickerview工具栏, 但单击完成按钮单击事件不起作用C# PickerView工具栏的“完成”按钮不工作,c#,ios,xamarin,uipickerview,toolbar,C#,Ios,Xamarin,Uipickerview,Toolbar,“完成”按钮添加到pickerview工具栏, 但单击完成按钮单击事件不起作用 public override void ViewDidLoad(){ myPickerView = new UIPickerView (RectangleF.Empty){ AutoresizingMask = UIViewAutoresizing.FlexibleWidth, ShowSelectionIndicator = true,
public override void ViewDidLoad(){
myPickerView = new UIPickerView (RectangleF.Empty){
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
ShowSelectionIndicator = true,
Model = model,
Hidden = true,
BackgroundColor = UIColor.Clear
};
toolbar = new UIToolbar();
toolbar.BarStyle = UIBarStyle.Default;
toolbar.Translucent = true;
toolbar.SizeToFit();
// Create a 'done' button for the toolbar and add it to the toolbar
UIBarButtonItem doneButton = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done,
(s, e) => {
Console.WriteLine ("Calling Done!");
txt_RegistrationType.Text = selectedType;
txt_Email.ResignFirstResponder();
txt_UserName.ResignFirstResponder();
txt_Password.ResignFirstResponder();
txt_PhoneNumber.ResignFirstResponder();
txt_ConformPassword.ResignFirstResponder();
});
toolbar.SetItems(new UIBarButtonItem[]{doneButton}, true);
model.PickerChanged += (sender, e) => {
Console.WriteLine("selected vlaue {0}",e.SelectedValue);
txt_RegistrationType.Text = e.SelectedValue;
};
this.myPickerView.AddSubview (toolbar);
myPickerView.Frame = PickerFrameWithSize (myPickerView.SizeThatFits (SizeF.Empty));
View.AddSubview (myPickerView);
myPickerView.AddSubview (toolbar);
}
单击所选项目
它显示pickerView“pickerView.Hidden=false”,其中显示了带有“完成”按钮的选择器视图和工具栏。单击工具栏上的“完成”按钮时,其单击事件不起作用
public override void ViewDidLoad(){
myPickerView = new UIPickerView (RectangleF.Empty){
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
ShowSelectionIndicator = true,
Model = model,
Hidden = true,
BackgroundColor = UIColor.Clear
};
toolbar = new UIToolbar();
toolbar.BarStyle = UIBarStyle.Default;
toolbar.Translucent = true;
toolbar.SizeToFit();
// Create a 'done' button for the toolbar and add it to the toolbar
UIBarButtonItem doneButton = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done,
(s, e) => {
Console.WriteLine ("Calling Done!");
txt_RegistrationType.Text = selectedType;
txt_Email.ResignFirstResponder();
txt_UserName.ResignFirstResponder();
txt_Password.ResignFirstResponder();
txt_PhoneNumber.ResignFirstResponder();
txt_ConformPassword.ResignFirstResponder();
});
toolbar.SetItems(new UIBarButtonItem[]{doneButton}, true);
model.PickerChanged += (sender, e) => {
Console.WriteLine("selected vlaue {0}",e.SelectedValue);
txt_RegistrationType.Text = e.SelectedValue;
};
this.myPickerView.AddSubview (toolbar);
myPickerView.Frame = PickerFrameWithSize (myPickerView.SizeThatFits (SizeF.Empty));
View.AddSubview (myPickerView);
myPickerView.AddSubview (toolbar);
}
单击“完成”按钮获取事件时,请告知我。首先,我想指出,您正在将“工具栏”添加到当前的“UIPickerView”中两次。一旦与:
this.myPickerView.AddSubview (toolbar);
另一次是:
myPickerView.AddSubview (toolbar);
其次,为什么使用工具栏?它只会增加项目的复杂性和依赖性
我的建议是废弃整个代码块,并在此视图上创建两个单独的控件:
public override void ViewDidLoad()
{
UIButton button = new UIButton (new RectangleF (5, 5, frame_width, 30));
button.TouchUpInside += Method;
UIPickerView pickerView = new UIPickerView (new RectangleF (5, 45, frame_width, 180));
pickerView.Model = model
this.View.AddSubviews (new UIView[]{button, pickerView});
}
void Method (object sender, eventargs e)
{
Console.WriteLine ("Done");
/*If you want to make the pickerView disappear,
you can add a pickerView.Hide() here, and then call a method
to redraw the controls on the current view.*/
}