Xamarin.Forms 中附加属性的绑定问题

Issue with bindings on attached properties in Xamarin.Forms

编辑:请不要post回答如何在Xamarin.Forms中实现手势——先阅读整个post。

我正在创建一个滑动手势处理程序作为使用附加属性的效果(如 Xamarin guides 中所述)。跳过方法讨论我有一个关于附加属性实现的奇怪问题。

长话短说(下面的代码)- XAML 到附加属性的绑定不起作用。我的静态 class 中的 Set\Get...Command 方法根本没有执行。我没有在应用程序输出中看到任何 Debug.WriteLine() 结果。调试器也不会命中那里设置的断点。与 ...CommandPropertyChanged() 处理程序相同。

这是我的 class 属性处理:

namespace Core.Effects
{
    public static class Gesture
    {
        public static readonly BindableProperty SwipeLeftCommandProperty =
            BindableProperty.CreateAttached("SwipeLeftCommand", typeof(ICommand), typeof(Gesture), null, propertyChanged: SwipeLeftCommandPropertyChanged);

        public static readonly BindableProperty SwipeRightCommandProperty =
            BindableProperty.CreateAttached("SwipeRightCommand", typeof(ICommand), typeof(Gesture), null, propertyChanged: SwipeRightCommandPropertyChanged);

        public static ICommand GetSwipeLeftCommand(BindableObject view)
        {
            Debug.WriteLine("GetSwipeLeftCommand");
            return (ICommand) view.GetValue(SwipeLeftCommandProperty);
        }

        public static void SetSwipeLeftCommand(BindableObject view, ICommand value)
        {
            Debug.WriteLine("SetSwipeLeftCommand");
            view.SetValue(SwipeLeftCommandProperty, value);
        }

        public static ICommand GetSwipeRightCommand(BindableObject view)
        {
            Debug.WriteLine("GetSwipeRightCommand");
            return (ICommand) view.GetValue(SwipeRightCommandProperty);
        }

        public static void SetSwipeRightCommand(BindableObject view, ICommand value)
        {
            Debug.WriteLine("SetSwipeRightCommand");
            view.SetValue(SwipeRightCommandProperty, value);
        }

        private static void SwipeLeftCommandPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            Debug.WriteLine("SwipeLeftCommandPropertyChanged");
            // ...
        }

        private static void SwipeRightCommandPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            Debug.WriteLine("SwipeRightCommandPropertyChanged");
            // ...
        }

        // ...
    }
}

下面是我在 XAML 中的使用方式:

<?xml version="1.0" encoding="UTF-8"?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:effects="clr-namespace:Core.Effects"
             x:Class="Core.Pages.RequestDetailsPage">
    <ContentPage.Content>
        <StackLayout Spacing="0"
                     Padding="0"
                     VerticalOptions="FillAndExpand"
                     effects:Gesture.SwipeLeftCommand="{Binding NavigateToPreviousRequestCommand}"
                     effects:Gesture.SwipeRightCommand="{Binding NavigateToNextRequestCommand}">

我的视图模型中有相应的命令(使用 FreshMvvm 框架实现的 MVVM):

namespace Core.PageModels
{
    public class RequestDetailsPageModel : FreshBasePageModel
    {
        public IRelayCommand NavigateToNextRequestCommand;
        public IRelayCommand NavigateToPreviousRequestCommand;

IRelayCommand 是我从 ICommand 派生的类型。 BindingContext 由 FreshMvvm 正确设置(在此视图的其他地方工作良好)。

知道哪里出了问题吗?

我已经在 GitHub 上完成了样本回购,检查下方 Link。

https://github.com/softlion/XamarinFormsGesture

https://github.com/tkowalczyk/SimpleCustomGestureFrame

http://arteksoftware.com/gesture-recognizers-with-xamarin-forms/

我的错误很简单(也很难追踪)。绑定不对字段进行操作,而是对属性进行操作:

namespace Core.PageModels
{
    public class RequestDetailsPageModel : FreshBasePageModel
    {
        public IRelayCommand NavigateToNextRequestCommand {get; set;}
        public IRelayCommand NavigateToPreviousRequestCommand {get; set;}