Xamarin Forms 渲染器不响应事件更改
Xamarin Forms renderer not responding to event change
我对 Xamarin 表单和自定义渲染器部分还很陌生。我已经为日期选择器编写了一个自定义渲染器,并且在 xamarin 表单中使用了相同的渲染器。
下面是我的 XAML 、ViewModel 和渲染器 类 的代码。
XAML :
<flightControls:CustomDatePicker x:Name="FromDatePicker" Date="{Binding FromDate}" >
<flightControls:CustomDatePicker.Behaviors>
<behaviour:CommandBehavior EventName="DateSelected" Command="{Binding FromDateSelectedCommand}" CommandParameter="{Binding Source={x:Reference FromDatePicker}}" />
</flightControls:CustomDatePicker.Behaviors>
</flightControls:CustomDatePicker>
查看模型
起始日期属性:
private DateTime _fromDate;
public DateTime FromDate
{
get
{
return (_fromDate == default(DateTime)) ? DateTime.Now : _fromDate;
}
set
{
_fromDate = (value == default(DateTime)) ? DateTime.Now : value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FromDate"));
}
}
FromDateSelectedCommand 命令:
public ICommand FromDateSelectedCommand { get; set; }
FromDateSelectedCommand = new DelegateCommand<object>(FromDateSelected);
private void FromDateSelected(object selectedDate)
{
FromDate = ((DatePicker)selectedDate).Date;
}
自定义日期选择器Class:
public class CustomDatePicker : DatePicker
{
}
CustomDatePickerRenderer UWP 渲染器:
public class CustomDatePickerRenderer:ViewRenderer<Xamarin.Forms.DatePicker,Windows.UI.Xaml.Controls.CalendarDatePicker>
{
CalendarDatePicker datePicker = new CalendarDatePicker();
/// <summary>
/// OnElement Changed method to get the Native DatePicker element
/// </summary>
/// <param name="e"></param>
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
datePicker.PlaceholderText = "Select Date";
datePicker.Date = e.NewElement.Date;
datePicker.PlaceholderText = "dd-mm-yyyy";
datePicker.DateChanged += DatePicker_DateChanged;
SetNativeControl(datePicker);
}
}
private void DatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
}
}
如何从渲染器获取更新的日期更改值并将其分配回我的视图模型的 FromDate 属性?因为 DateChanged 命令根本没有被调用。我怀疑必须通过返回一个值来对渲染器的日期更改事件做一些事情。
现在 FromDate 属性 始终具有默认值 DateTime.Now 并且在选择器中更改日期时不会更新。
如有任何帮助,我们将不胜感激。
找到解决方案。如下设置值确实触发了 DateChanged 事件绑定到的命令。在我的 CustomDatePickerRenderer DateChanged 事件中,我不得不写下一行
private void DatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
// This line triggered the DateChanged event in view model.
Element.Date = args.NewDate.Value.Date;
}
我对 Xamarin 表单和自定义渲染器部分还很陌生。我已经为日期选择器编写了一个自定义渲染器,并且在 xamarin 表单中使用了相同的渲染器。
下面是我的 XAML 、ViewModel 和渲染器 类 的代码。
XAML :
<flightControls:CustomDatePicker x:Name="FromDatePicker" Date="{Binding FromDate}" >
<flightControls:CustomDatePicker.Behaviors>
<behaviour:CommandBehavior EventName="DateSelected" Command="{Binding FromDateSelectedCommand}" CommandParameter="{Binding Source={x:Reference FromDatePicker}}" />
</flightControls:CustomDatePicker.Behaviors>
</flightControls:CustomDatePicker>
查看模型
起始日期属性:
private DateTime _fromDate;
public DateTime FromDate
{
get
{
return (_fromDate == default(DateTime)) ? DateTime.Now : _fromDate;
}
set
{
_fromDate = (value == default(DateTime)) ? DateTime.Now : value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FromDate"));
}
}
FromDateSelectedCommand 命令:
public ICommand FromDateSelectedCommand { get; set; }
FromDateSelectedCommand = new DelegateCommand<object>(FromDateSelected);
private void FromDateSelected(object selectedDate)
{
FromDate = ((DatePicker)selectedDate).Date;
}
自定义日期选择器Class:
public class CustomDatePicker : DatePicker
{
}
CustomDatePickerRenderer UWP 渲染器:
public class CustomDatePickerRenderer:ViewRenderer<Xamarin.Forms.DatePicker,Windows.UI.Xaml.Controls.CalendarDatePicker>
{
CalendarDatePicker datePicker = new CalendarDatePicker();
/// <summary>
/// OnElement Changed method to get the Native DatePicker element
/// </summary>
/// <param name="e"></param>
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
datePicker.PlaceholderText = "Select Date";
datePicker.Date = e.NewElement.Date;
datePicker.PlaceholderText = "dd-mm-yyyy";
datePicker.DateChanged += DatePicker_DateChanged;
SetNativeControl(datePicker);
}
}
private void DatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
}
}
如何从渲染器获取更新的日期更改值并将其分配回我的视图模型的 FromDate 属性?因为 DateChanged 命令根本没有被调用。我怀疑必须通过返回一个值来对渲染器的日期更改事件做一些事情。
现在 FromDate 属性 始终具有默认值 DateTime.Now 并且在选择器中更改日期时不会更新。
如有任何帮助,我们将不胜感激。
找到解决方案。如下设置值确实触发了 DateChanged 事件绑定到的命令。在我的 CustomDatePickerRenderer DateChanged 事件中,我不得不写下一行
private void DatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
// This line triggered the DateChanged event in view model.
Element.Date = args.NewDate.Value.Date;
}