I followed the steps from the accepted answer from this link.I did my "own implementation", but the command doesn't raise. Also, I did the same as the post said, but it didn't work.
Here is the XAML code : OrdenesPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="ControYaApp.Views.OrdenesPage"
xmlns=";
xmlns:x=";
xmlns:model="clr-namespace:ControYaApp.Models"
xmlns:toolkit=";
xmlns:viewModel="clr-namespace:ControYaApp.ViewModels"
x:DataType="viewModel:OrdenesViewModel">
<ContentPage.Behaviors>
<toolkit:EventToCommandBehavior Command="{Binding AppearingCommand}" EventName="Appearing" />
</ContentPage.Behaviors>
... more code
Code-behind: OrdenesPage.xaml.cs
public partial class OrdenesPage : ContentPage
{
public OrdenesPage(OrdenesViewModel vm, AppShellViewModel appShellvm)
{
InitializeComponent();
BindingContext = vm;
ImageButton_FlyoutShell.Command = appShellvm.FlyoutShellCommand;
}
}
ViewModel (It is no all code, it is a simple version):
OrdenesViewModel.cs
public ICommand AppearingCommand { get; }
public OrdenesViewModel(RestService restService, OrdenRepo ordenRepo, EmpleadosRepo empleadosRepo)
{
AppearingCommand = new RelayCommand(Appearing)
}
private void Appearing()
{
try
{
// Do something
}
catch (Exception ex)
{
Toast.Make(ex.Message).Show();
}
}
Here is DI MauiProgram.cs
... omitted for brevity
public static MauiAppBuilder RegisterViewModels(this MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddTransient<OrdenesViewModel>();
}
public static MauiAppBuilder RegisterViews(this MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddTransient<OrdenesPage>();
}
I have set breakpoint to Appearing()
, but it doesn't stop there.
Did I forget something?
I followed the steps from the accepted answer from this link.I did my "own implementation", but the command doesn't raise. Also, I did the same as the post said, but it didn't work.
Here is the XAML code : OrdenesPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="ControYaApp.Views.OrdenesPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:model="clr-namespace:ControYaApp.Models"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:viewModel="clr-namespace:ControYaApp.ViewModels"
x:DataType="viewModel:OrdenesViewModel">
<ContentPage.Behaviors>
<toolkit:EventToCommandBehavior Command="{Binding AppearingCommand}" EventName="Appearing" />
</ContentPage.Behaviors>
... more code
Code-behind: OrdenesPage.xaml.cs
public partial class OrdenesPage : ContentPage
{
public OrdenesPage(OrdenesViewModel vm, AppShellViewModel appShellvm)
{
InitializeComponent();
BindingContext = vm;
ImageButton_FlyoutShell.Command = appShellvm.FlyoutShellCommand;
}
}
ViewModel (It is no all code, it is a simple version):
OrdenesViewModel.cs
public ICommand AppearingCommand { get; }
public OrdenesViewModel(RestService restService, OrdenRepo ordenRepo, EmpleadosRepo empleadosRepo)
{
AppearingCommand = new RelayCommand(Appearing)
}
private void Appearing()
{
try
{
// Do something
}
catch (Exception ex)
{
Toast.Make(ex.Message).Show();
}
}
Here is DI MauiProgram.cs
... omitted for brevity
public static MauiAppBuilder RegisterViewModels(this MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddTransient<OrdenesViewModel>();
}
public static MauiAppBuilder RegisterViews(this MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddTransient<OrdenesPage>();
}
I have set breakpoint to Appearing()
, but it doesn't stop there.
Did I forget something?
Share Improve this question asked Feb 6 at 13:36 L_JL_J 72 bronze badges 1- I created a new project to test the command and the dependency injection. The appearing method will be hit in my project. Can you show more details to reproduce this problem? – Liyun Zhang - MSFT Commented 2 days ago
1 Answer
Reset to default 0Try either:
- Moving the
BindingContext
initialization beforeInitializeComponent();
public OrdenesPage(OrdenesViewModel vm, AppShellViewModel appShellvm)
{
BindingContext = VM; // <--- Move here
InitializeComponent();
// BindingContext = VM;
- Adding
OnPropertyChanged(nameof(AppearingCommand));
public OrdenesViewModel(RestService restService, OrdenRepo ordenRepo, EmpleadosRepo empleadosRepo)
{
AppearingCommand = new RelayCommand(Appearing)
OnPropertyChanged(nameof(AppearingCommand)); // <-- Add this (N.B. I'm assuming you have CommunityToolkit.Mvvm or similar to do like this)
}