Xamarin.Forms中的Picker上的OnChangeListener

时间:2017-09-07 09:04:14

标签: c# xamarin.forms picker

我有两个选择器,我从选择器获得了每个SelectedItem,但是每当我改变它时,他们都不会触发我的方法值为什么?

以下是我的代码:

这是我如何宣布每个选择器的选定指数:

int selectedIndexYear = year_pk.SelectedIndex;
int selectedIndexTerm = term_pk.SelectedIndex;

以下是每当我更改选择器时触发的逻辑,但它不起作用:

if (selectedIndexYear != -1 && selectedIndexTerm != -1)
{
    int term = Int32.Parse(term_pk.SelectedItem.ToString());
    //Below  is my method am Firing Each change of the Picker.
    OnResultsList(year_pk.SelectedItem.ToString(), term);
}

1 个答案:

答案 0 :(得分:0)

根据那种情况,我发现只要在选择器上选择了一个项目的更改,我就会启动一个事件。

我建议您使用SelectedIndexChanged事件,如下所示:

year_pk.SelectedIndexChanged += delegate 
{
    int term = Int32.Parse(term_pk.SelectedItem.ToString());
    //Below  is my method am Firing Each change of the Picker.
    OnResultsList(year_pk.SelectedItem.ToString(), term);
}

term_pk.SelectedIndexChanged += delegate 
{
    int term = Int32.Parse(term_pk.SelectedItem.ToString());
    //Below  is my method am Firing Each change of the Picker.
    OnResultsList(year_pk.SelectedItem.ToString(), term);
}