如何在UIPickerViewModel中修复Override方法GetTitle

时间:2019-01-14 20:21:39

标签: c# xamarin.ios

我必须用一些名称填充UIPickerView,运行模拟时在选择器中看不到这些名称。

似乎根本没有使用getTitle超越方法。我不知道为什么。我在取景器中什么也没得到

这是我的模型课

 public class MotifPickerModel : UIPickerViewModel
     {
        //Attributes
         List<String> names = new List<string>();

    //constructors
    public MotifPickerModel()
    {
        names.Add("name1");
        names.Add("name2");
        names.Add("name3");
        names.Add("name4");
    }
    public MotifPickerModel(List<String> Names)
    {
        this.names = Names;
    }
    public override nint GetRowsInComponent(UIPickerView pickerView, nint component)
    {
        return names.Count;
    }

    public override nint GetComponentCount(UIPickerView pickerView)
    {
        return 1;
    }
    public override string GetTitle(UIPickerView pickerView, nint row, nint component)
    {

        return names[(int)row].ToString();
    }

    public override nfloat GetComponentWidth(UIPickerView pickerView, nint component)
    {
        if (component == 0)
            return 220f;
        else
            return 30f;
    }

}

在这里我虚化了uipicker并调用模型 我使用文本输入来调用屏幕上的uipickerview。而点击手势识别器可以让我在屏幕上其他任何地方点击时将其消失。

public override void ViewDidLoad()
    {
        base.ViewDidLoad();


        var pickerMotif = new UIPickerView();
        //Here we must populate the picker
        var pickermodel = new MotifPickerModel();
        pickerMotif.Model = pickermodel;

        txtMotif.InputView = pickerMotif;

        var tapGestureRecognizer = new UITapGestureRecognizer(Tap);
        tapGestureRecognizer.NumberOfTapsRequired = 1;
        View.AddGestureRecognizer(tapGestureRecognizer);

    }

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。我找到了对我有帮助的链接。 这里是: How to use PickerView as Combobox