为什么绑定不更新选择器选择项

时间:2017-10-20 10:33:19

标签: c# xamarin binding picker

我的Xamarin表格中有Picker,绑定到模型(下面的代码)。

Load方法设置SelectedVehicle,但选择器不显示所选车辆。首次加载页面时,选择器会在列表中显示正确的项目。但是在App.VehicleId更改后的页面重新加载时,选择器显示为空白。

即使我在SelectedIndex期间在选择器上明确设置OnAppearing,选择器显示为空白,SelectedIndex在我稍后查看时已设置回-1

如何在重新加载页面时正确更新选择器选择?

的Xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:DefectReport"
             x:Class="DefectReport.MainPage">
    <ContentPage.Content>
        <ScrollView>
            <StackLayout>
                <Label x:Name="Message" TextColor="Red" />
                <Label Text="Welcome to Vehicle Check" />
                <Label Text="Choose vehicle" />
                <Picker ItemsSource="{Binding Vehicles}" ItemDisplayBinding="{Binding RegistrationNumber}" SelectedItem="{Binding SelectedVehicle}" SelectedIndexChanged="Picker_SelectedIndexChanged" />
                <Label Text="{Binding SelectedVehicle.Description}" />
                <Button Text="Add vehicle not in list" Clicked="SelectVehicle_Clicked" />
                <Button Text="Check vehicle" Clicked="CheckVehicle_Clicked" />
                <Button Text="Log out" Clicked="LogOut_Clicked" />
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

背后的代码是:

public partial class MainPage : ContentPage {
    public class VehicleInfo : Model {
        public async void Load() {
            Vehicles = new ObservableCollection<Vehicle>(await App.Database.ReadAsync<Vehicle>().ToListAsync());
            SelectedVehicle = Vehicles.FirstOrDefault(v => v.DeviceRecordId == App.VehicleId) ?? Vehicles.FirstOrDefault();
            System.Diagnostics.Debug.WriteLine("App.VehicleId = " + App.VehicleId);
            System.Diagnostics.Debug.WriteLine("SelectedVehicle.Id = " + selectedVehicle.DeviceRecordId);
        }

        private Vehicle selectedVehicle;

        public Vehicle SelectedVehicle {
            get { return selectedVehicle; }
            set {
                if (selectedVehicle != value) {
                    selectedVehicle = value;
                    OnPropertyChanged("SelectedVehicle");
                }
            }
        }

        private ObservableCollection<Vehicle> vehicles;

        public ObservableCollection<Vehicle> Vehicles {
            get { return vehicles; }
            set {
                if (vehicles != value) {
                    vehicles = value;
                    OnPropertyChanged("Vehicles");
                }
            }
        }

    }
    VehicleInfo data;
    public MainPage() {
        data = new VehicleInfo();
        BindingContext = data;
        InitializeComponent();
    }

    protected override void OnAppearing() {
        base.OnAppearing();
        data.Load();
    }

Model是一个实现INotifyPropertyChanged的简单类:

public class Model : System.ComponentModel.INotifyPropertyChanged {
    public void OnPropertyChanged(string name) {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

1 个答案:

答案 0 :(得分:0)

事实证明问题在于创建一个新的Vehicles = new ObservableCollection<Vehicle>(await App.Database.ReadAsync<Vehicle>().ToListAsync()); 。我改变了代码

@WebService(
  targetNamespace = "http://com.redsigil/soapserver",
  name = "com.redsigil.soapserver",
  portName = "soap",
  serviceName = "SoapServerSample")
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
@Addressing(enabled = true, required = false)
class SoapServerImpl() {
  @WebMethod(action = "hashing")
  def test(@WebParam(name = "mystr", mode = WebParam.Mode.IN) value: String): String = sha256Hash(value)

  def sha256Hash(text: String): String =
    String.format("%064x",
      new java.math.BigInteger(1, java.security.MessageDigest.getInstance("SHA-256").digest(text.getBytes("UTF-8"))))
}

而是清除现有的集合,并将新记录添加到其中。