I am new using .NET MAUI and my issue is very simple but I could not find a way to solve this, I am trying to get any component by its x:Name using Mvvm pattern.
is it possible through ViewModels? For example I have a login page after clicking button I want this button to get blocked to prevent double clicks.
Login.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Ventas_Citel.Views.Login.Login"
xmlns:viewmodel="clr-namespace:Sales.ViewModels.Login"
Title="">
<VerticalStackLayout
//... login page xaml form ...
<Button Text="Log In" WidthRequest="200" CornerRadius="5" HorizontalOptions="Center" Command="{Binding LoginCommand}" x:Name="loginButton"/>
//... login page xaml form ...
</VerticalStackLayout>
Login.xaml.cs
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Maui.Alerts;
public partial class Login : ContentPage
{
public Login(LoginViewModel viewModel)
{
InitializeComponent();
BindingContext = viewModel;
}
}
loginViewModel
public partial class LoginViewModel : ObservableObject
{
[RelayCommand]
async void Login()
{
if (!string.IsNullOrWhiteSpace(Email) && !string.IsNullOrWhiteSpace(Password))
{
// is there a way to call loginButton component?????
// i need to disable it with loginButton.IsEnable = false;
loginButton.IsEnable = false // doesnt work loginButton doesnt exist
// if there is any error i need to enable it again so the user can re enter
// his/her credentials if it fails
loginButton.IsEnable = true;
}
}
}
How do I solve this?