加载ViewModel时,在Picker和DatePicker上绑定默认值

时间:2019-06-07 09:47:00

标签: xaml xamarin mvvm xamarin.forms prism

在加载import tensorflow as tf randint = tf.round(tf.random.uniform(shape=(1, 10))) with tf.Session() as sess: for _ in range(5): print(randint.eval()) print() # [[1. 0. 1. 1. 1. 1. 1. 1. 1. 1.]] # # [[1. 1. 0. 0. 1. 0. 0. 0. 1. 0.]] # # [[1. 1. 0. 0. 1. 1. 0. 0. 0. 1.]] # # [[0. 0. 0. 1. 1. 0. 1. 0. 1. 1.]] # # [[1. 1. 1. 0. 0. 0. 0. 1. 1. 1.]] 类时,我想在DatePicker上设置选定的日期,在Picker上设置选定的项目。我怎么做

ViewModel

这是我的XAMl

public override void OnNavigatedTo(INavigationParameters parameters)
{
    base.OnNavigatedTo(parameters);
    var objL = parameters.GetValue<LeaveManagement>("LeaveItem");
    SelectedFromDate = objL.StartDate;
    SelectedToDate = objL.EndDate;
    SelectedLeaveType = new SpinnerBind { Name = objL.LeaveTypeCD, Value = objL.LeaveTypeId };           
}

private DateTime? _selectedFromDate;
public DateTime? SelectedFromDate
{
    get { return _selectedFromDate; }
    set { _selectedFromDate = value; }
}
private DateTime? _selectedToDate;
public DateTime? SelectedToDate
{
    get { return _selectedToDate; }
    set { _selectedToDate = value; }
}

private ObservableCollection<SpinnerBind> _leaveType;
public ObservableCollection<SpinnerBind> LeaveType
{
    get { return _leaveType; }
    set
    {
        SetProperty(ref _leaveType, value);
    }
}
private SpinnerBind _selectedLeaveType;
public SpinnerBind SelectedLeaveType
{
    get { return _selectedLeaveType; }
    set
    {
        SetProperty(ref _selectedLeaveType, value);
    }
}

但是未设置所有值Startdate,Enddate和SelectedLeaveType。但是<RelativeLayout> <DatePicker Format="dd/MM/yyyy" Date="{Binding SelectedFromDate}"/> <DatePicker Format="dd/MM/yyyy" Date="{Binding SelectedToDate}"/> <Picker SelectedItem="{Binding SelectedLeaveType, Mode=TwoWay}" ItemsSource="{Binding LeaveType,Mode=TwoWay}" TitleColor="Black" TextColor="Black" Title="--Select--"/> </RelativeLayout> Picker绑定如何解决此问题?

1 个答案:

答案 0 :(得分:1)

当视图模型的属性更改时,它需要提高INotifyPropertyChanged.PropertyChanged才能更新绑定。

Prism为视图模型提供了BindableBase基类,该类具有SetProperty方法,通常用于更新背景字段并在需要时引发事件。

也就是说,您想要这样的东西

public DateTime? SelectedFromDate
{
    get { return _selectedFromDate; }
    set { SetProperty( ref _selectedFromDate, value ); }
}

编辑:要将Picker与默认选定项绑定,请从Source中初始化SelectedLeaveType