最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mvvm - Property Command from EventToCommandBehavior didn't work - Stack Overflow

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

Try either:

  1. Moving the BindingContext initialization before InitializeComponent();
    public OrdenesPage(OrdenesViewModel vm, AppShellViewModel appShellvm)
    {
        BindingContext = VM; // <--- Move here
        InitializeComponent();
        // BindingContext = VM;
  1. 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)
}
发布评论

评论列表(0)

  1. 暂无评论