ManaevRN
ManaevRN

Reputation: 15

Turning Event to Command issue

i'm trying to turn Entry.Completed Event to Command, using MVVM. I found this article: https://devblogs.microsoft.com/xamarin/turn-events-into-commands-behaviors/ and tried to make something similar, but in Behavior.cs file in Bindable_Completed function Command is always passing as null, i don't understand why, please help. Behavior file code:

public class EntryCompletedBehavior : Behavior<Entry>
{
    protected override void OnAttachedTo(Entry bindable)
    {
        base.OnAttachedTo(bindable);

        AssociatedObject = bindable;

        bindable.BindingContextChanged += Bindable_BindingContextChanged;
        bindable.Completed += Bindable_Completed;
    }
    protected override void OnDetachingFrom(Entry bindable)
    {
        base.OnDetachingFrom(bindable);

        bindable.BindingContextChanged -= Bindable_BindingContextChanged;
        bindable.Completed -= Bindable_Completed;

        AssociatedObject = null;
    }

    private void Bindable_BindingContextChanged(object sender, System.EventArgs e)
    {
        OnBindingContextChanged();
    }
    private void Bindable_Completed(object sender, System.EventArgs e)
    {
        if (Command == null) return;

        //object parameter = Converter.Convert(e, typeof(object), null, null);
        if (Command.CanExecute(e))
        {
            Command.Execute(e);
        }

    }

    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();
        BindingContext = AssociatedObject.BindingContext;
    }

    public Entry AssociatedObject { get; private set; }
    public static readonly BindableProperty CommandProperty =
            BindableProperty.Create("Command", typeof(ICommand), typeof(EntryCompletedBehavior), null);
    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }
}

Xaml code:

    <Grid BindableLayout.ItemsSource="{Binding ActualValues}"
          Grid.Column="0"
          Grid.ColumnSpan="2"
          Grid.Row="0">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <Frame Margin="{Binding Margin}">
                    <Entry TabIndex="{Binding Id}"
                           Text="{Binding Value}"
                           Placeholder="{Binding Placeholder}"
                           Style="{StaticResource EntryStyle}">
                        <Entry.Behaviors>
                            <behaviors:EntryCompletedBehavior Command="{Binding EntryCompletedCommand}"/> 
                        </Entry.Behaviors>
                    </Entry>
                </Frame>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </Grid>

VM code:

public SaveMeasurementViewModel()
        {
            SaveMeasurmentCommand = new Command(async () => await SaveMeasurment(), () => !IsBusy);
            ClearMeasurmentCommand = new Command(ClearMeasurment);
            EntryCompletedCommand = new Command(_ => EntryCompleted(_));

            ActualValues = new ObservableCollection<MyEntry>() { };
            for (int index = 0; index < numberOfMeasurments; index++)
            {
                RegisterObserver(new MyEntry(index, this));
            }
        }
  public System.Windows.Input.ICommand EntryCompletedCommand { get; protected set; }
    void EntryCompleted(object param)
    {
        Application.Current.MainPage.DisplayAlert("Save", "Measurment data has been saved", "OK");
    }

Upvotes: 0

Views: 78

Answers (1)

ManaevRN
ManaevRN

Reputation: 15

As the canton7 said, this problem was causing because of the BindableLayout.ItemsSource, i was trying to find property not in whole VM class, but in specified ItemSource. I checked this command binding outside of the BindableLayout.ItemsSource, and it worked fine.

Upvotes: 1

Related Questions