.net maui MVVM exit button wont file but collection button does. what did I miss?

tim 140 Reputation points
2024-05-20T14:54:19.5166667+00:00

I'm converting a xaml app to .net maui.

I'm not able to find where I'm missing code that is keeping the button from firing the exit code in my viewmodel.

i've compared the code for the exit page to my collection page ( In which the button works) but I not able to see what I've left out of the exit code to have the app trigger the exit code.

both pages inherited from BaseViewModel

xaml page

<?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"

         x:Class="MapleSugarMaui.Views.ExitAppPage"

         xmlns:local="clr-namespace:MapleSugarMaui"

         xmlns:vm="clr-namespace:MapleSugarMaui.ViewModels"

         x:DataType="vm:ExitAppViewModel"

         xmlns:models="clr-namespace:MapleSugarMaui.Models"

         NavigationPage.HasNavigationBar="False">

<ContentPage.BindingContext>

    <vm:ExitAppViewModel/>

</ContentPage.BindingContext>

<ContentPage.Content>

    <StackLayout>

        <StackLayout Orientation="Horizontal" HeightRequest="45">

            <Entry Placeholder="StatusMsg"  Text="{Binding StatusMsg}" FontSize="15"  Margin="150,0,0,0" 

                     HorizontalTextAlignment="Center" TextColor="Red" HorizontalOptions="Center" WidthRequest="500"

                        IsVisible="True"/>

        </StackLayout>

        <StackLayout Orientation="Vertical" HeightRequest="100" >

            <Label Text="Close App?" HorizontalOptions="CenterAndExpand" FontSize="30" TextColor="Red" FontAttributes="Bold" Margin="0,10,0,0"/>

            <StackLayout Orientation="Horizontal" HeightRequest="45" Margin="50">

                <Button     Text="Close" 

                            HorizontalOptions="CenterAndExpand" 

                            WidthRequest="150"  

                            HeightRequest="50" 

                            VerticalOptions="CenterAndExpand" 

                            BackgroundColor="White" 

                            TextColor="Black" 

                            BorderWidth="2"  

                            BorderColor="Black" 

                            CornerRadius="15"

                            Command="{Binding ExitAppActionCommand}"/>

            </StackLayout >

        </StackLayout>

    </StackLayout>

</ContentPage.Content>

</ContentPage>

xaml.cs page

using MapleSugarMaui.ViewModels;

namespace MapleSugarMaui.Views;

public partial class ExitAppPage : ContentPage

{

public ExitAppPage()

{

	InitializeComponent();

    BindingContext = new ExitAppViewModel();

}

}

ViewModel Page

using CommunityToolkit.Mvvm.Input;

namespace MapleSugarMaui.ViewModels

{

public partial class ExitAppViewModel : BaseViewModel

{

    public ExitAppViewModel()

    {

        StatusMsg = "Close ??";

    }

    [RelayCommand]

    private void ExitAppAction()

    {

        StatusMsg = "Closing App";

        System.Environment.Exit(0);

    }

}

}

TIA

Tim

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,724 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,017 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,921 Reputation points Microsoft Vendor
    2024-05-21T02:46:08.67+00:00

    Hello,

    Please remove HeightRequest="100" in <StackLayout Orientation="Vertical" HeightRequest="100" > line. After that, this ExitAppActionCommand will be invoked.

    If you set it, Button cannot be clicked (Button is out of the StackLayout due to the Margin="50" of the <StackLayout >), but it appears in your screen.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.