Xamarin,有什么方法可以在用户改变主意时更新选择器上的列表?

时间:2019-05-31 04:05:02

标签: xamarin xamarin.forms xamarin.forms.listview

,而不点击完成。例如,用户首先选择2,因此列表更新1,3,4在选择器2上可用,但是当我将其更改为1时,则单击完成3和4是唯一可用的更新。即使没有在Picker上按DONE也会触发每次更新列表。

我似乎找不到合适的方法或接近解决方案

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace demo
{
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(true)]
public partial class MainPage : ContentPage
{
List<string> list;

public MainPage()
{
InitializeComponent();
list = new List<string>();

list.Add("1");
list.Add("2");
list.Add("3");
list.Add("4");
drainxy1();
drainxy2();
drainxy3();
drainxy4();


drainlocationPicker1.SelectedIndexChanged += 
drain1Handle_SelectedIndexChanged;
drainlocationPicker2.SelectedIndexChanged += 
drain2Handle_SelectedIndexChanged;         
drainlocationPicker3.SelectedIndexChanged += 
drain3Handle_SelectedIndexChanged;
drainlocationPicker4.SelectedIndexChanged += 
drain4Handle_SelectedIndexChanged;
}

private void drain4Handle_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void drain3Handle_SelectedIndexChanged(object sender, EventArgs e)
{
drainlocationPicker1.IsEnabled = false;
drainlocationPicker2.IsEnabled = false;
drainlocationPicker4.Items.Remove((string)drainlocationPicker3.SelectedIte
m);

}

private void drain2Handle_SelectedIndexChanged(object sender, EventArgs e)
{
drainlocationPicker1.IsEnabled = false;

drainlocationPicker3.Items.Remove((string)drainlocationPicker2.SelectedIte
m);            
drainlocationPicker4.Items.Remove((string)drainlocationPicker2.SelectedIte
m);

}

private void drain1Handle_SelectedIndexChanged(object sender, EventArgs e)
{

drainlocationPicker2.Items.Remove((string)drainlocationPicker1.SelectedItem);            
drainlocationPicker3.Items.Remove((string)drainlocationPicker1.SelectedItem);           
drainlocationPicker4.Items.Remove((string)drainlocationPicker1.SelectedItem);
}

void drainxy1()
{
foreach (var item in list)
{
    drainlocationPicker1.Items.Add(item);
}
}
void drainxy2()
{

foreach (var item in list)
{
    drainlocationPicker2.Items.Add(item);
}
}
void drainxy3()
{

foreach (var item in list)
{
    drainlocationPicker3.Items.Add(item);
}
}
void drainxy4()
{

foreach (var item in list)
{
drainlocationPicker4.Items.Add(item);
}
}
}
}

1 个答案:

答案 0 :(得分:0)

这是iOS上选择器的默认行为。如果您只想在点击“完成”后进行更新,则需要为每个选择器添加它:

pickerName.On<iOS>().SetUpdateMode(UpdateMode.WhenFinished);,其中pickerName是在您的xaml中分配给选择器的x:Name="pickerName"

这也可以在xaml中完成,但是有点混乱,并添加了一个永久的蓝色花键,这使我很烦。

现在,在按下完成键之前,选择器不会更改该值。