【问题标题】:Bind Xuni Calendar SelectionChanging event to ViewModel (Xamarin Forms with Prism)将 Xuni Calendar SelectionChanging 事件绑定到 ViewModel (Xamarin Forms with Prism)
【发布时间】:2017-10-19 02:39:46
【问题描述】:

我想在我的 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 没有被触发。

谢谢, 乌韦

【问题讨论】:

    标签: xaml xamarin mvvm xamarin.forms prism


    【解决方案1】:

    您实际上不需要创建转换器,您只需要指定命令、事件名称和 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&lt;DateTime&gt; 所以你需要在你的 ViewModel 中进行以下操作

    public DelegateCommand<List<DateTime>> SelectionChangingCommand { get; }
    
    public void OnSelectionChangingCommandExecuted(List<DateTime> selectedDates)
    {
        // Do stuff
    }
    

    【讨论】:

    • 效果很好。谢谢!我只需要将“Path”替换为“EventArgsParameterPath”即可运行。
    猜你喜欢
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多