kevindang
kevindang

Reputation: 37

Xamarin Forms + Mvvmcross binding command not work

I am an new Xamarin Form. I created a simple xamarin forms project with mvvmcross (Hello World very simple for begin), but when i implemented binding command, and not effect change text of label. My Xaml code and ViewModel below.

<?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:vm="clr-namespace:MvvmCross.ViewModels;assembly=MvvmCross"
         x:Class="MvvmCross.Views.HelloView">
<StackLayout>
    <StackLayout.BindingContext>
        <vm:HelloViewModel />
    </StackLayout.BindingContext>
    <Entry  HorizontalOptions="Fill" VerticalOptions="Center" Text="{Binding Name, Mode=TwoWay }"/>
    <Button Text="Hello" HorizontalOptions="Center" VerticalOptions="Center" Command="{Binding HelloCommand}" />
    <Label HorizontalOptions="Fill" VerticalOptions="Center" FontSize="15" Text="{Binding Hello, Mode=TwoWay}" />
</StackLayout>

using MvvmCross.Core.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MvvmCross.ViewModels
{
   public class HelloViewModel: Core.ViewModels.MvxViewModel
   {
    private string _name;
    public HelloViewModel()
    {
        Hello = "Your name";
    }
    public string Name
    {
        get { return _name; }
        set { _name = value; RaisePropertyChanged(() => Name); }
    }
    private string _hello;

    public string Hello
    {
        get { return _hello; }
        set { _hello = value; RaisePropertyChanged(() => Hello); }
    }

    private ICommand _helloCommand;

    public ICommand HelloCommand
    {
        get { _helloCommand = _helloCommand ?? new MvxCommand(ShowHello); return _helloCommand; }
    }

    private void ShowHello()
    {
        // not change label text so sadly
        Hello = Name.ToString();
        Debug.WriteLine(Hello);
    }
}

}

Thank for all helping

Upvotes: 0

Views: 944

Answers (2)

AndreaGobs
AndreaGobs

Reputation: 386

Even if late, it could help someone else.

if you have set up correctly MvvmCross on your Xamarin Forms project (review [Getting Started with MvvmCross][1]) you don't need to specifically set the BindigContext, neither in the view nor in the view model.

About the question, simple example of the use of the button's command binding:

  • view

    <views:MvxContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                      xmlns:views="clr- namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
                      x:Class="TestProject.Pages.TestPage">
    <ContentView>
        <StackLayout>
            <Button Text="Test first command!" Command="{Binding TestFirstCommand}"/>
            <Button Text="Test second command!" Command="{Binding TestSecondCommand}"/>
            <Label Text="{Binding AnyText}"/>
        </StackLayout>
    </ContentView>
    

  • view model

    namespace TestProject.ViewModels 
    {
        public class TestViewModel : MvxNavigationViewModel
        {
            private string _AnyTest;
    
            public TestViewModel()
            {
                AnyText = "";
            }
    
            public string AnyText { get => _AnyTest; set => SetProperty(ref _AnyTest, value); }
            public Command TestFirstCommand => new Command(TestFirstCommandMethod);
            public Command TestSecondCommand => new Command(TestSecondCommandMethod);
    
            private void TestFirstCommandMethod()
            {
                AnyText = "Hello!";
            }
            private void TestSecondCommandMethod()
            {
                AnyText = "How are you?";
            }
        }
    }
    

Upvotes: 0

Farid
Farid

Reputation: 1020

Has u set the BindingContext?

In your HelloView.xaml.cs:

public HelloView() {
    BindingContext = new HelloViewModel();
}

I'm on mobile, really hard to type..

Upvotes: -1

Related Questions