How to create Relay command in Master Page ViewModel

Sowndarrajan Vijayaragavan 330 Reputation points
2023-06-09T07:26:53.1566667+00:00

MASTER PAGE(XAML)

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:lang ="clr-namespace:ZolaApplication"
             x:Class="ZolaApplication.View.MasterPage.MasterPageContent"
             Title="MasterPageContent">
    <ContentPage.ControlTemplate>
        <ControlTemplate>
            <Grid RowDefinitions="64,*">
                <Grid Grid.Row="0" ColumnDefinitions="*,*,*"  BackgroundColor="#F1F4F6" >
				<ImageButton Source="test.png" text ="TRAIL"
                                          Scale="0.5" Command="testCommand" />
                 </Grid>   
                <ContentPresenter  Grid.Row="1" IsVisible="true"/>
            </Grid>
        </ControlTemplate>
    </ContentPage.ControlTemplate>
</ContentPage>

MASTER PAGE(XAML) .cs

namespace ZolaApplication.View.MasterPage;
public partial class MasterPageContent : ContentPage 
{  
    public MasterPageContentViewModel viewModel;       
    public MasterPageContent()    
    {         
        InitializeComponent();        
        viewModel = new MasterPageContentViewModel();        
        BindingContext = viewModel;         
    }   
}

Calling in another Final page (XAML)

<controls:MasterPageContent ......
	<ContentPage.Resources>
	</ContentPage.Resources>
</controls:MasterPageContent>

Final page (XAML).cs

public partial class FinalPage: MasterPageContent
{ 
    public FinalViewModel _viewModel;
	{
	InitializeComponent();
	_viewModel = new FinalViewModel();
	this.BindingContext = _viewModel;
	}
}

Masterpage (ViewModel)

using CommunityToolkit.Mvvm.ComponentModel; 
using CommunityToolkit.Mvvm.Input;  
namespace ZolaApplication.ViewModel 
{
    public partial class  MasterPageContentViewModel :  ObservableObject    
    {                  
        public MasterPageContentViewModel()         
        {  
        }         

        [RelayCommand]        
        public void Test()        
        { 
           int trail = 1;       
        }     
    } 
}

[RELAY COMMAND] is not trigerring

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,861 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rob Caplan - MSFT 5,412 Reputation points Microsoft Employee
    2023-06-09T20:24:05.16+00:00

    I edited your post to be more readable.

    In the future, please minimize your code to just what is needed to demonstrate the problem. Please provide details on the versions of Maui and the target platforms your have the problem on, as well as a clear explanation of the repro steps, expected behaviour, and actual behaviour.

    It's much easier to help you if we know where the problem is that you're asking for help on.

    I suspect that you would like ImageButton's Command to trigger the Test RelayCommand, but it's not because the Command isn't connected properly. It needs to bind to the TestCommand rather than being set to a string.

    Something like:

      <Button Text="Execute test command"
            Command="{Binding TestCommand}" />
    

    Please see the Maui Commanding documentation for a full overview of how Commanding works.

    If you need more explanation, please edit the question to clarify more clearly what you're doing and where you still need help

    0 comments No comments