c# - PickerView 工具栏的完成按钮不起作用

标签 c# ios xamarin uipickerview toolbar

添加到 pickerview 工具栏的完成按钮, 但是点击完成按钮点击事件不起作用。

    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”,它显示选择器 View 和带有完成按钮的工具栏。单击工具栏上的完成按钮时,其单击事件不起作用。

请让我知道如何在点击完成按钮上获取事件。

最佳答案

首先,我想指出您两次将“工具栏”添加到当前的“UIPickerView”。一旦:

this.myPickerView.AddSubview (toolbar);

还有一次:

myPickerView.AddSubview (toolbar);

其次,为什么是工具栏?它只会增加项目的复杂性和依赖性。

我的建议是废弃整个代码块,并在此 View 上制作两个单独的控件:

  1. 一个按钮
  2. 一个 UIPickerView

    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.*/
    }
    

希望对您有所帮助。祝你好运!

关于c# - PickerView 工具栏的完成按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26065940/

相关文章:

ios - 无法使用 Xcode 8.0 在 Xcode 项目中为第一个版本创建具有匹配 Bundle ID 的 App ID

c# - 在 Xamarin 中为 UITextField 正确设置背景图片

c# - CS0117 C# 'Resource.Id' 不包含 "PhoneNumberText"的定义

c# - 使用 Frame.Navigate 和 typeof(UserControl)

c# - 在类而不是类的集合上使用 foreach

ios - 在 rails 中显示 base64 编码的图像

ios - Xamarin iOS 使用 Jenkins 和 Nuget 在 OSX 上构建服务器

c# - X509Certificate2 构造函数中的 "The specified network password is not correct"异常

c# - 从标准 io 编码捕获输出?

ios - tvO 上的侧键盘