繁体   English   中英

具有Bindable属性的自定义视图在Xamarin.Forms SAP上无法正确绑定

[英]Custom View with Bindable Property not binding properly on Xamarin.Forms SAP

我有一个复选框,它应该触发按钮的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方法永远不会被执行,因此我想要设置的按钮属性永远不会改变。

在此先感谢大家!

你可以做的是将这个问题的解决方案分为几个层次:

首先,为复选框创建一个可绑定属性,并将其命名为Checked。 我已经为它写了一个snippit,但没有真正编译它所以你可能想要修改它,如果它不起作用

  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");
    }
} 

第三,将复选框“isChecked”的可绑定属性绑定到xaml中视图模型中的属性:

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

第四,Xaml与MVVM中的按钮绑定命令。 在这些命令中,有一个bool属性代表“CanExecute”,它基本上启用或禁用按钮。 所以你在Xaml中要做的就是将按钮的命令绑定到View Model中的命令(让我们称之为ClickCommand)。 ClickCommand的CanExecute实际上只是一个返回“IsChecked”值的方法。 这将使我们修改“IsChecked”属性的setter,因此每次更改时都应该通知命令检查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> 

使用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
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM