1

For some context, I'm using Microsoft's MVVM Toolkit and .Net Maui.

Im trying to bind a RelayCommand to a Command Bindable object of a view. The code builds fine, it's at runtime that I'm seeing the following error: "Warning: 'DeleteCommand' property not found on 'MyApp.PageModels.MainPageModel', target property: 'MyApp.Views.NavigationBar.RightButtonCommand'"

Here's the code:

MainPage.xaml

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:pageModels="clr-namespace:MyApp.PageModels"
             xmlns:views="clr-namespace:MyApp.Views"
             NavigationPage.HasNavigationBar="False"
             BackgroundColor="#4044C9"
             x:Class="MyApp.Pages.MainPage"
             x:DataType="pageModels:MainPageModel" >

    <StackLayout>
        <views:NavigationBar
            Title="{Binding Total, StringFormat='{0:C}'}"
            RightButtonImageSource="add"
            RightButtonCommand="{Binding DeleteCommand}"/>

        <!-- Other code -->

    </StackLayout>
</ContentPage>

MainPageModel.cs

public partial class MainPageModel : ObservableObject
{
    [ObservableProperty]
    double total;

    [RelayCommand]
    async Task Delete()
    {
        //execute delete
    }
}

NavigationBar.xaml.cs

public partial class NavigationBar : ContentView
{
    // BindableProperty for the right image button's command
    public static readonly BindableProperty RightButtonCommandProperty =
        BindableProperty.Create(nameof(RightButtonCommand), typeof(Command), typeof(NavigationBar), default(Command), BindingMode.OneWayToSource);

    // Right button command property
    public Command RightButtonCommand
    {
        get => (Command)GetValue(RightButtonCommandProperty);
        set => SetValue(RightButtonCommandProperty, value);
    }
}

I binded the variable Total to the view's Label and it show without problems. The issue is with the RelayCommand DeleteCommand.

Eliezer Ferrá
  • 127
  • 1
  • 12

1 Answers1

0

Why is the binding mode OneWayToSource? I suspect binder then looks for a setter on viewmodel.DeleteCommand, which it fails to find - the command can only be read from viewmodel.

Fix: Change RightButtonCommandPropertys BindingMode to OneWay.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196