Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xaml 如何将日期选择器绑定到viewModel?_Xaml_Mvvm_Xamarin.forms - Fatal编程技术网

Xaml 如何将日期选择器绑定到viewModel?

Xaml 如何将日期选择器绑定到viewModel?,xaml,mvvm,xamarin.forms,Xaml,Mvvm,Xamarin.forms,如何从DatePicker获取值并在ViewModel的变量中设置它? 在我的xaml文件中,我有如下日期选择器: 在我的ViewModel中,我有属性 public DateTime DateTo{get;set;} 并在InidData方法中设置其值: DateTo=DateTime.Now.Date.AddDays(1); 但是,当日期更改并且单击选择器上的“确定”按钮时,如何更改该值呢?您的XAML看起来不错 但是,ViewModel中的属性没有调用PropertyChanged

如何从DatePicker获取值并在ViewModel的变量中设置它? 在我的xaml文件中,我有如下日期选择器:


在我的ViewModel中,我有属性

public DateTime DateTo{get;set;}
并在InidData方法中设置其值:

DateTo=DateTime.Now.Date.AddDays(1);
但是,当日期更改并且单击选择器上的“确定”按钮时,如何更改该值呢?

您的XAML看起来不错

但是,ViewModel中的属性没有调用
PropertyChanged
。当ViewModel中属性的值更改时,它需要调用
PropertyChanged?。调用
以警告/更新视图

下面是XAML的一个示例,它的代码隐藏和ViewModel:

XAML

代码隐藏
公共部分类MyView
{
初始化组件();
BindingContext=新的MyViewModel();
}
视图模型
公共类MyViewModel:INotifyPropertyChanged
{
bool_dateToPickerVisibility
DateTime\u dateTo
公共事件属性更改事件处理程序属性更改;
公共日期时间日期到
{
get=>\u dateTo;
设置
{
_dateTo=值;
PropertyChanged?.Invoke(这是新的propertychangedventargs(nameof(DateTo));
}
}
公共图书馆DateToPickerVisibility
{
get=>\u dateToPickerVisibility;
设置
{
_dateToPickerVisibility=值;
PropertyChanged?.Invoke(这是新的propertychangedventargs(nameof(DateToPickerVisibility));
}
}
}

是的,您的ViewModel需要实现
INotifyPropertyChanged
,以便日期选择器可以自动更新该值

有一个完整的演示,可以正常工作。主要代码是:

 public class DateViewModel: INotifyPropertyChanged
{
    bool _dateToPickerVisibility;

    public bool DateToPickerVisibility
    {
        set { SetProperty(ref _dateToPickerVisibility, value); }
        get { return _dateToPickerVisibility; }
    }

    DateTime _dateTo;
    public DateTime DateTo
    {
        set { SetProperty(ref _dateTo, value); }
        get { return _dateTo; }
    }

    public DateViewModel()
    {
        DateTo = DateTime.Now.Date.AddDays(1);
        DateToPickerVisibility = true;
    }

    bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        if (Object.Equals(storage, value))
            return false;

        storage = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
注意:

1.您可以在ViewModel的构造函数中初始化该值

 public DateViewModel()
    {
        DateTo = DateTime.Now.Date.AddDays(1);
        DateToPickerVisibility = true;
    }
2.为了使日期选择器可见或隐藏,我添加了一个按钮来更改
DateToPickerVisibility
的值

  private void MButton_Clicked(object sender, EventArgs e)
    {
        if (dateViewModel.DateToPickerVisibility) {
            dateViewModel.DateToPickerVisibility = false;
            mButton.Text = "Make DatePicker visible";
        }
        else {
            dateViewModel.DateToPickerVisibility = true;
            mButton.Text = "Hide DatePicker";
        }
    }
结果是:


您似乎做得很正确。它不工作吗?日期在显示时设置正确,所以它就像占位符一样,但当选择另一个日期并单击“确定”按钮时,如何获取它的值?如何将一些命令绑定到datepicker?您可能需要查看MVVM的工作方式。您的视图模型需要实现INotifyPropertyChanged,否则UI不知道什么时候发生了什么。当用户更新日期选择器时,绑定机制应该自动更新
DateTo
。这就是使用数据绑定的全部意义,以使开发人员不必手动操作。@AndresCastro我看到了,但我的ViewModel已经继承了另一个模型,而另一个模型alreadz继承了另一个模型…:我想用这种方式实施起来会很困难。
 public DateViewModel()
    {
        DateTo = DateTime.Now.Date.AddDays(1);
        DateToPickerVisibility = true;
    }
  private void MButton_Clicked(object sender, EventArgs e)
    {
        if (dateViewModel.DateToPickerVisibility) {
            dateViewModel.DateToPickerVisibility = false;
            mButton.Text = "Make DatePicker visible";
        }
        else {
            dateViewModel.DateToPickerVisibility = true;
            mButton.Text = "Hide DatePicker";
        }
    }