将 Xuni Calendar SelectionChanging 事件绑定到 ViewModel(Xamarin Forms with Prism)
Bind Xuni Calendar SelectionChanging event to ViewModel (Xamarin Forms with Prism)
我想在我的 Xamarin Forms 应用程序 (Prism) 中使用 Xuni calendar control。
我如何使用 Prism 将日历控件的 SelectionChanging 事件绑定到我的 ViewModel 中的命令,因为我不想使用代码隐藏。
这是我的 XAML 到目前为止。
<xuni:XuniCalendar x:Name="calendar" MaxSelectionCount="-1" Grid.Row="0" Grid.ColumnSpan="2">
<xuni:XuniCalendar.Behaviors>
<b:EventToCommandBehavior EventName="SelectionChanging" Command="{Binding SelectionChangingCommand}"
EventArgsConverter="{StaticResource selectionChangingEventArgsConverter}" />
</xuni:XuniCalendar.Behaviors>
</xuni:XuniCalendar>
这是我的转换器:
public class SelectionChangingEventArgsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var selectionChangingEventArgs = value as CalendarSelectionChangingEventArgs;
if (selectionChangingEventArgs == null)
{
throw new ArgumentException("Expected value to be of type SelectionChangingEventArgs", nameof(value));
}
return selectionChangingEventArgs.SelectedDates;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这是我的 ViewModel 中的命令:
public DelegateCommand SelectionChangingCommand => new DelegateCommand(SelectionChanging);
private void SelectionChanging()
{
throw new NotImplementedException();
}
我没有收到任何错误,但未触发 ViewModel 中的 SelectionChangingCommand。
谢谢,
乌维
您实际上不需要创建转换器,您只需要指定命令、事件名称和 EventArgs 路径SelectedDates
。
<xuni:XuniCalendar MaxSelectionCount="-1"
Grid.Row="0"
Grid.ColumnSpan="2">
<xuni:XuniCalendar.Behaviors>
<b:EventToCommandBehavior EventName="SelectionChanging"
Command="{Binding SelectionChangingCommand}"
Path="SelectedDates" />
</xuni:XuniCalendar.Behaviors>
</xuni:XuniCalendar>
在您的 ViewModel 中,您需要使用通用的 DelegateCommand 来接受参数。根据 docs SelectedDates 是 List<DateTime>
因此您需要在 ViewModel
中添加以下内容
public DelegateCommand<List<DateTime>> SelectionChangingCommand { get; }
public void OnSelectionChangingCommandExecuted(List<DateTime> selectedDates)
{
// Do stuff
}
我想在我的 Xamarin Forms 应用程序 (Prism) 中使用 Xuni calendar control。 我如何使用 Prism 将日历控件的 SelectionChanging 事件绑定到我的 ViewModel 中的命令,因为我不想使用代码隐藏。 这是我的 XAML 到目前为止。
<xuni:XuniCalendar x:Name="calendar" MaxSelectionCount="-1" Grid.Row="0" Grid.ColumnSpan="2">
<xuni:XuniCalendar.Behaviors>
<b:EventToCommandBehavior EventName="SelectionChanging" Command="{Binding SelectionChangingCommand}"
EventArgsConverter="{StaticResource selectionChangingEventArgsConverter}" />
</xuni:XuniCalendar.Behaviors>
</xuni:XuniCalendar>
这是我的转换器:
public class SelectionChangingEventArgsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var selectionChangingEventArgs = value as CalendarSelectionChangingEventArgs;
if (selectionChangingEventArgs == null)
{
throw new ArgumentException("Expected value to be of type SelectionChangingEventArgs", nameof(value));
}
return selectionChangingEventArgs.SelectedDates;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这是我的 ViewModel 中的命令:
public DelegateCommand SelectionChangingCommand => new DelegateCommand(SelectionChanging);
private void SelectionChanging()
{
throw new NotImplementedException();
}
我没有收到任何错误,但未触发 ViewModel 中的 SelectionChangingCommand。
谢谢, 乌维
您实际上不需要创建转换器,您只需要指定命令、事件名称和 EventArgs 路径SelectedDates
。
<xuni:XuniCalendar MaxSelectionCount="-1"
Grid.Row="0"
Grid.ColumnSpan="2">
<xuni:XuniCalendar.Behaviors>
<b:EventToCommandBehavior EventName="SelectionChanging"
Command="{Binding SelectionChangingCommand}"
Path="SelectedDates" />
</xuni:XuniCalendar.Behaviors>
</xuni:XuniCalendar>
在您的 ViewModel 中,您需要使用通用的 DelegateCommand 来接受参数。根据 docs SelectedDates 是 List<DateTime>
因此您需要在 ViewModel
public DelegateCommand<List<DateTime>> SelectionChangingCommand { get; }
public void OnSelectionChangingCommandExecuted(List<DateTime> selectedDates)
{
// Do stuff
}