Souta Norogami
Souta Norogami

Reputation: 3

Why does xamarin not use commands from view model?

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; }
    }
}

Upvotes: 0

Views: 188

Answers (3)

Guangyu Bai - MSFT
Guangyu Bai - MSFT

Reputation: 4511

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; }
    }

Upvotes: 0

Sugitha
Sugitha

Reputation: 163

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");
                });
            }
        }

    }
}

Upvotes: 2

Skittles86
Skittles86

Reputation: 106

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");
            });

Upvotes: 0

Related Questions