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

时间:2017-07-07 09:07:52

标签: xaml mvvm xamarin.forms attached-properties

编辑:请不要发布有关如何在Xamarin.Forms中实现手势的答案 - 请先阅读整篇文章。

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

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

这是我的属性处理类:

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正确设置(在此视图的其他位置效果很好)。

任何可能出错的线索?

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

我的错误很简单(很难跟踪)。绑定不在字段上操作,而是在属性上操作:

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