4

ボタンの IsEnabled イベントを発生させるはずのチェックボックスがあります。しかし、どういうわけか、それを行うはずのコマンドが適切にバインドされず、実行されません。

これは、CheckBox.xaml.cs (コントロール) のバインド可能なプロパティです。

public static readonly BindableProperty CheckBoxCommandProperty =
       BindableProperty.Create<CheckBox, ICommand>(
       checkbox =>
           checkbox.CheckBoxCommand,
           null,
           propertyChanged: (bindable, oldValue, newValue) =>
               {
                   CheckBox checkbox = (CheckBox)bindable;
                   EventHandler<bool> eventHandler = checkbox.CheckedChanged;
                   if (eventHandler != null)
                   {
                       eventHandler(checkbox, checkbox.IsChecked);
                   }
               });

   public event EventHandler<bool> CheckedChanged;

   public ICommand CheckBoxCommand
   {
       get { return (ICommand)GetValue(CheckBoxCommandProperty); }
       set { SetValue(CheckBoxCommandProperty, value); }
   }

そして、これは私がViewModelに持っているものです:

   public ICommand Next_OnClick { get; set; }
   public ICommand OnCheckBoxTapChanged { get; set; }

   public TermsAndConditionsViewModel()
   {
       Next_OnClick = new Command(NextClicked);
       OnCheckBoxTapChanged = new Command(CheckBoxTapped);
   }

   private void CheckBoxTapped()
   {
       if (IsCheckedChanged)
       {
           Next_IsEnabled = true;
       }
       else
       {
           Next_IsEnabled = false;
       }
   }

CheckBoxTapped メソッドは実行されないため、設定したいボタン プロパティは変更されません。

よろしくお願いします!

4

2 に答える 2

2

あなたができることは、この問題の解決策をいくつかのレベルに分割することです:

最初に、チェックボックスのバインド可能なプロパティを作成し、それを Checked と呼びます。私はそのスニピットを書きましたが、実際にはコンパイルしていないので、機能しない場合は変更することをお勧めします

  public static readonly BindableProperty IsCheckedProperty = 
    BindableProperty.Create<CheckBox, bool> (w => w.IsChecked, false);

  public bool IsChecked{
    get { return GetValue (FooProperty); }
    set { SetValue (FooProperty, value); } 
  }

次に、ビュー モデルで、変更通知を持つ bool プロパティを作成します。

private bool _isChecked;
public bool IsChecked
{
    get 
    { 
        return _isChecked;
    }
    set
    {
        _isChecked = value;
        RaisePropertyChanged("IsChecked");
    }
} 

3 番目に、チェック ボックス「isChecked」のバインド可能なプロパティを、xaml のビュー モデルのプロパティにバインドします。

<StackLayout>
    <Checkbox IsChecked = "{Binding IsChecked}"/>
</StackLayout>

第 4 に、MVVM を使用した Xaml のボタンはコマンドにバインドされます。これらのコマンドには、基本的にボタンを有効または無効にする「CanExecute」を表す bool プロパティがあります。したがって、Xaml で行う必要があるのは、ボタンのコマンドをビュー モデルのコマンドにバインドすることです (ClickCommand と呼びましょう)。ClickCommand の CanExecute は、実際には「IsChecked」の値を返す単なるメソッドです。これにより、「IsChecked」プロパティのセッターが変更されるため、変更されるたびにコマンドに通知して CanExecute プロパティをチェックする必要があります。

したがって、最終的なコードは次のようになります

public TermsAndConditionsViewModel()
   {
       NextCommand = new Command(ExecuteNextCommand,CanExecuteNextCommand);
       OnCheckBoxTapChanged = new Command(CheckBoxTapped);
   }


private bool _isChecked;
public bool IsChecked
{
    get { return _isChecked;}
    set
    {
         _isChecked = value;
    NextCommand.ChangeCanExecute(); //this will actually notify the command to enable/disable
        RaisePropertyChanged("IsChecked");
    }
}


public Command NextCommand {get;set;} // this type is available in Xamarin.Forms library
private bool CanExecuteNextCommand()
{
    return IsChecked;
}
private void ExecuteNextCommand()
{
    // your execution when the button is pressed    
}

そしてXamlは次のようになります

<StackLayout>
    <Checkbox IsChecked = "{Binding IsChecked}"/>
    <Button Text= "Next" Command = "{Binding NextCommand}"/>
</StackLayout> 
于 2015-07-30T21:28:59.753 に答える
1

Xamarin.Forms.Behavior を使用して解決しました。コントロールへの複数のバインディングを許可します。

例えば>

<Entry Placeholder="Enter password"
             Text= "{Binding TxtFirstPasswordInput}"
             IsPassword="true">
        <b:Interaction.Behaviors>
          <b:BehaviorCollection>
            <b:EventToCommand EventName="Unfocused" Command="{Binding entry_Finished}"/>
            <b:EventToCommand EventName="Focused" Command="{Binding entry_Focused}"/>
          </b:BehaviorCollection>
        </b:Interaction.Behaviors>
      </Entry>

そしてViewModelで>

public PasswordInputViewModel()
        {
            entry_Finished = new Command(validateAndContinue);//unfocused
            entry_Focused = new Command(entryFocused); //focused
        }
于 2015-08-13T14:45:51.547 に答える