Home > Software design >  Why does xamarin not use commands from view model?
Why does xamarin not use commands from view model?

Time:06-29

Xamarin doesn't use my commands from view models. Events work, but it ignores my commands. I've checked, commands were initialized, but still doesn't work. I thought it was only in my working project, but also in the test project I created an hour ago.

My test view:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewmodels="clr-namespace:TestXamarinMvvm.ViewModels"
             x:DataType="viewmodels:MainVM"
             x:Class="TestXamarinMvvm.MainPage">

    <ContentPage.BindingContext>
        <viewmodels:MainVM/>
    </ContentPage.BindingContext>
    
    <StackLayout>
        <Button Text="Test"
                Command="{Binding TestCommand}"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand"/>
    </StackLayout>

</ContentPage>

My test view model:

namespace TestXamarinMvvm.ViewModels
{
    internal class MainVM : BaseVM
    {
        public MainVM()
        {
            TestCommand = new RelayCommand(obj =>
            {
                App.Current.MainPage.DisplayAlert("Work", "It is working.", "OK");
            });
        }

        public RelayCommand TestCommand { get; }
    }
}

CodePudding user response:

I made a demo on my side and it works well.

Here is the code in MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodels="clr-namespace:App7"
             x:Class="App7.MainPage">

    <ContentPage.BindingContext>
        <viewmodels:MainVM/>
    </ContentPage.BindingContext>

    <StackLayout>
        <Button Text="Test"
                Command="{Binding TestCommand}"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand"/>
    </StackLayout>

</ContentPage>

Here is the code in the ViewModel MainVM.cs:

 internal class MainVM 
    {
        public MainVM()
        {
            TestCommand = new Command(() =>
            {
                App.Current.MainPage.DisplayAlert("Work", "It is working.", "OK");
            });
        }
        public ICommand TestCommand { get; set; }
    }

CodePudding user response:

I'm not sure what a RelayCommand is, but it seems to expect an obj parameter.

Maybe this func will work?

TestCommand = new RelayCommand(() =>
            {
                App.Current.MainPage.DisplayAlert("Work", "It is working.", "OK");
            });

CodePudding user response:

Im not sure how the relay commands work. But I usually use this command for button and it works for me. Please check if it helps.

namespace TestXamarinMvvm.ViewModels
{
    internal class MainVM : BaseVM
    {
      public ICommand TestCommand
        {
            get
            {
                return new Command((args) =>
                {
                    App.Current.MainPage.DisplayAlert("Work", "It is working.", "OK");
                });
            }
        }

    }
}
  • Related