2016-07-13 121 views
3

我有一個PCL,我正在使用MVVM模式。 我正在構建一個togglebutton,它在viewmodel中的相關屬性被激活時觸發,具有行爲和觸發器。 這是我的行爲Xamarin形式ToggleButton

public class ToggleBehavior : Behavior<View> 
{ 
    TapGestureRecognizer tapRecognizer; 

    public static readonly BindableProperty IsToggledProperty = BindableProperty.Create<ToggleBehavior, bool>(tb => tb.IsToggled, false); 

    public bool IsToggled 
    { 
     set { SetValue(IsToggledProperty, value); } 
     get { return (bool)GetValue(IsToggledProperty); } 
    } 
} 

這是我如何消費自己的行爲

       <Button Text="AUTO" Style="{StaticResource SmallEllipseButton}" BackgroundColor="{StaticResource BackgroundColor}" cm:Message.Attach="Automatic($dataContext)"> 
           <Button.Behaviors> 
            <local:ToggleBehavior x:Name="autoToggleBehavior" IsToggled="{Binding CurrentMode,Converter={StaticResource modeToBooleanConverter}, ConverterParameter=AUTO}"/> 
           </Button.Behaviors> 
           <Button.Triggers> 
            <DataTrigger TargetType="Button" Binding="{Binding Source={x:Reference autoToggleBehavior},Path=IsToggled}" Value="False" > 
            <Setter Property="BackgroundColor" Value="White"/> 
            <Setter Property="BorderColor" Value="White"/> 
            <Setter Property="TextColor" Value="Gray"/> 
            </DataTrigger> 
            <DataTrigger TargetType="Button" Binding="{Binding Source={x:Reference autoToggleBehavior},Path=IsToggled}" Value="True" > 
            <Setter Property="BackgroundColor" Value="{StaticResource BackgroundColor}"/> 
            <Setter Property="TextColor" Value="White"/> 
            </DataTrigger> 
           </Button.Triggers> 
          </Button> 

的問題是,物業IsToggled未正確在此IsToggled="{Binding CurrentMode,Converter={StaticResource modeToBooleanConverter}, ConverterParameter=AUTO}"/> 如果我將它設置靜態爲true或false綁定有用。 我認爲的問題是,我無法將這個屬性用dinamically綁定。 我在同一頁面使用與同一個轉換器的IsVisible屬性相同的綁定,並且它可以工作。 如果我在轉換器中放置一個斷點,應用程序不會中斷它,但IsVisible屬性會中斷。

回答

1

使用按鈕可能不是最好的選擇,因爲它已經有一個水龍頭處理程序,並將其分配給它仍然不會觸發事件。我不知道這是否有幫助,但我將控件更改爲標籤以使下面的工作。當然,如果Button綁定到修改視圖模型的命令,那麼您根本不需要tap控制器。

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:local="clr-namespace:BehTest" x:Class="BehTest.BehTestPage"> 

    <Label Text="AUTO" HorizontalOptions="Center" VerticalOptions="Center"> 
     <Label.Behaviors> 
      <local:ToggleBehavior x:Name="autoToggleBehavior" IsToggled="{Binding Toggled, Mode=TwoWay}"/> 
     </Label.Behaviors> 
     <Label.Triggers> 
      <DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference autoToggleBehavior},Path=IsToggled}" Value="False" > 
      <Setter Property="BackgroundColor" Value="White"/> 
      <Setter Property="TextColor" Value="Gray"/> 
      </DataTrigger> 
      <DataTrigger TargetType="Label" Binding="{Binding Source={x:Reference autoToggleBehavior},Path=IsToggled}" Value="True" > 
      <Setter Property="BackgroundColor" Value="Blue"/> 
      <Setter Property="TextColor" Value="White"/> 
      </DataTrigger> 
     </Label.Triggers> 
    </Label> 
</ContentPage> 

行爲:

public class ToggleBehavior : Behavior<View> 
{ 
    readonly TapGestureRecognizer tapRecognizer; 

    public ToggleBehavior() 
    { 
     tapRecognizer = new TapGestureRecognizer 
     { 
      Command = new Command(() => this.IsToggled = !this.IsToggled) 
     }; 
    } 

    public static readonly BindableProperty IsToggledProperty = BindableProperty.Create<ToggleBehavior, bool>(tb => tb.IsToggled, false); 

    public bool IsToggled 
    { 
     set { SetValue(IsToggledProperty, value); } 
     get { return (bool)GetValue(IsToggledProperty); } 
    } 

    protected override void OnAttachedTo(View bindable) 
    { 
     base.OnAttachedTo(bindable); 
     bindable.GestureRecognizers.Add(this.tapRecognizer); 
    } 

    protected override void OnDetachingFrom(View bindable) 
    { 
     base.OnDetachingFrom(bindable); 
     bindable.GestureRecognizers.Remove(this.tapRecognizer); 
    } 

    protected override void OnAttachedTo(BindableObject bindable) 
    { 
     base.OnAttachedTo(bindable); 
     this.BindingContext = bindable.BindingContext; 
     bindable.BindingContextChanged += Bindable_BindingContextChanged; 
    } 

    protected override void OnDetachingFrom(BindableObject bindable) 
    { 
     base.OnDetachingFrom(bindable); 
     this.BindingContext = null; 
     bindable.BindingContextChanged -= Bindable_BindingContextChanged; 
    } 

    void Bindable_BindingContextChanged(object sender, EventArgs e) 
    { 
     var bobject = sender as BindableObject; 

     this.BindingContext = bobject?.BindingContext; 
    } 
} 
標籤爲 「IsToggled」
+0

結合還不能正常工作。它只有在我將其設置爲true/false時纔有效,但似乎我無法將其綁定到視圖模型屬性 – Marco24690

+0

上面的代碼確實對我有用。訣竅是從可綁定對象設置OnAttachedTo(BindableObject)上的綁定上下文。這對我來說似乎是一種破綻,但我懷疑行爲不是被設計成可綁定的,或者是XF代碼中存在一個錯誤。您可能想在Bugzilla中創建一個錯誤報告。 – SKall

+0

我沒有添加OnAttachedTo方法。現在它也適用於Button。謝謝 – Marco24690