2017-07-07 20 views
0

編輯:請不要發佈如何在Xamarin.Forms中實現手勢的答案 - 首先閱讀整篇文章。Xamarin.Forms中附加屬性的綁定問題

我使用附加屬性(如Xamarin guides中所述)創建滑動手勢處理程序作爲效果。跳過方法討論,我有一個附加屬性實現的奇怪問題。

長話短說(代碼如下) - XAML綁定到附加屬性不起作用。我的靜態類中的​​方法根本不被執行。我沒有看到任何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}"> 

我在我的視圖模型(MVVM與FreshMvvm執行相應的命令框架):

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

IRelayCommand是我的類型衍生自ICommand。 FreshMvvm正確設置了BindingContext(在此視圖的其他位置運行良好)。

任何線索可能是錯誤的?

回答

0

我的錯誤是非常簡單的(和相當困難的跟蹤其一)。綁定不上的字段,但對性能進行操作:

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

謝謝你的回答,但我的問題不是如何實現滑動手勢(我已經看到了這些解決方案,他們幫助我實現其他部分),但* *爲什麼附加的屬性不工作** 關於第一個回購 - 由於[仍然開放的問題](https://github.com/softlion/XamarinFormsGesture/issues/2) – Marek

+0

您是否創建了渲染器,它不起作用? – Pratik

+0

不,我不知道。請仔細閱讀完整的問題(和我的評論) – Marek