admin管理员组

文章数量:1277896

I have a xaml file with 2 basic buttons: button_1 and Button_2

The problem, is that the first button is responsive, but Button_2 is not.

However, if I move Button_2 above Button_1 in the xaml file, then Button_2 becomes responsive while button_1 is not anymore.

Any idea what I am doing wrong here?

I havent added the VM,as 2 methods are very distinct from each other so I don't think it this matters. SOmething tells me its coming from the xaml file.

I tried to wrap them in <StackLayout>, just in case the behaviour might change but no.

xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns=";
             xmlns:x=";
             x:Class="App._Views.FolderName.View"


            <!-- BUtton 1-->
            <StackLayout/>

                    <Button 
                         Command="{Binding Button1Command}"
                         CommandParameter="{Binding .}" />

            <!-- BUtton 2-->

                    <Button
                     Command="{Binding Button2Command}"
                     CommandParameter="{Binding .}" />

            </StackLayout>
</ContentPage>

ViewModel - using breapoint, Button2() is not accessed.

      public partial class VM: ObservableObject, IQueryAttributable
      {
    
    
            [RelayCommand]
            private async void Button1()
            {
                Console.WriteLine("Button1 is accessed");
    
            }
    
    
            [RelayCommand]
            private async void Button2()
            {
                Debug.WriteLine("Button 2 is clicked")
}

   }

I have a xaml file with 2 basic buttons: button_1 and Button_2

The problem, is that the first button is responsive, but Button_2 is not.

However, if I move Button_2 above Button_1 in the xaml file, then Button_2 becomes responsive while button_1 is not anymore.

Any idea what I am doing wrong here?

I havent added the VM,as 2 methods are very distinct from each other so I don't think it this matters. SOmething tells me its coming from the xaml file.

I tried to wrap them in <StackLayout>, just in case the behaviour might change but no.

xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft/winfx/2009/xaml"
             x:Class="App._Views.FolderName.View"


            <!-- BUtton 1-->
            <StackLayout/>

                    <Button 
                         Command="{Binding Button1Command}"
                         CommandParameter="{Binding .}" />

            <!-- BUtton 2-->

                    <Button
                     Command="{Binding Button2Command}"
                     CommandParameter="{Binding .}" />

            </StackLayout>
</ContentPage>

ViewModel - using breapoint, Button2() is not accessed.

      public partial class VM: ObservableObject, IQueryAttributable
      {
    
    
            [RelayCommand]
            private async void Button1()
            {
                Console.WriteLine("Button1 is accessed");
    
            }
    
    
            [RelayCommand]
            private async void Button2()
            {
                Debug.WriteLine("Button 2 is clicked")
}

   }
Share Improve this question edited Feb 24 at 11:46 PhilM asked Feb 24 at 11:15 PhilMPhilM 4052 silver badges15 bronze badges 4
  • 3 Your XAML appears to be invalid: you've got <StackLayout/> and then you later "close" that self-closing element just above the Grid-closing part.) (Why are you nesting StackLayouts and all, and why have two StackLayout elements with a single element each?) – Jon Skeet Commented Feb 24 at 11:20
  • 1 (Rather than assuming what matters and what doesn't, I would strongly encourage you to come up with a minimal but complete example. I suspect you could provide much simpler XAML that would demonstrate the problem - or you'd find the problem while trying to get to that simpler XAML.) – Jon Skeet Commented Feb 24 at 11:21
  • If you're judging "responsiveness" on debug logging alone, I notice that in one case you're logging to the Console stream and in the other you're logging to the Debug stream. Where are you expecting to see the Console output? – IV. Commented Feb 24 at 16:13
  • I cannot reproduce this issue as your code works for me. Could you please share more xaml code? – Liqun Shen-MSFT Commented Feb 27 at 7:00
Add a comment  | 

1 Answer 1

Reset to default 2

It will work if you give the method the correct signature so it can generate the RelayCommand.

Avoid async void and use async Task instead, or in this case, you don't run async code, so just use a void method.

[RelayCommand]
private void Button1()
{
    Console.WriteLine("Button1 is accessed");
} 

alternative:

[RelayCommand]
private async Task Button1Async()
{
    Console.WriteLine("Button1 is accessed");
} 

本文标签: cMAUIButtons interfere with each otherStack Overflow