Simple Button Command won't fire - who can see the mistake?

215 views Asked by At

I cannot see where I am doing the mistake. Normally I've implemented everything properly to use the button command in my Xamarin.Forms AppShell project but the button doesn't fire. Maybe someone can see the issue?

I don't know if it's relevant though page/view of the button is a child. I am navigating to it with a TabBar from the parent view like this:

AppShell.xaml:

xmlns:local="clr-namespace:LVNGtoGo.Views"

<TabBar>
<ShellContent Title="Home" Icon="home_icon.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />
<ShellContent Title="B O M" Icon="new_icon.png" Route="NewBomPage" ContentTemplate="{DataTemplate local:NewBomPage}"  />
</TabBar>

BaseViewModel with INotifyPropertyChanged:

public class BaseViewModel : INotifyPropertyChanged
    {
      
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            var changed = PropertyChanged;
            if (changed == null)
                return;

            changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
            }

ViewModel inheriting from BaseViewModel and Constructor for the Command:

public class NewBomViewModel : BaseViewModel
    {
        private string text;
        public ICommand RallyCarCommand { get; }
        

        public NewBomViewModel()
        {
            RallyCarCommand = new Command(ChooseRallyCar);
        }

        private void ChooseRallyCar()
        {
            text = "Rally Car was chosen!!!";
        }

        public string Text 
        {
            get => text;
            set => SetProperty(ref text, value);
        }
}

XAML with Binding Context:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="LVNGtoGo.Views.NewBomPage"
             xmlns:vm="clr-namespace:LVNGtoGo.ViewModels"
             Shell.PresentationMode="ModalAnimated"
             Title="Create new  B O M">

    <ContentPage.BindingContext >
        <vm:NewBomViewModel />
    </ContentPage.BindingContext>

    <StackLayout Spacing="3" Padding="15" >

        <Label Text="{Binding Text}" VerticalOptions="Center" Margin="20" />
       
        <Button Text="Click me" Command="{Binding RallyCarCommand}" />

    </StackLayout>
</ContentPage>
1

There are 1 answers

1
Jason On BEST ANSWER

you are setting the private internal field, not the public property

text = "Rally Car was chosen!!!";

should be

Text = "Rally Car was chosen!!!";