ICommand doesn't work under VS2017 for Mac with iOS11?

tjr77tjr77 thierry thierryFRMember ✭✭

Hi developers,

I had created a button with the "ICommand" function.

Problem : By press the button link to the command on the iPhone simulator, nothing
happen and I didn't switch on my Login program.

I use VS2017 for MAC

Remarks : The same program running well on a PC with windows 7

As anybody get an issue please?

XAML code:

<Button Command="{**Binding LoginCommand **}" 
  BackgroundColor="Navy" BorderRadius="23"
  FontAttributes="Bold" HeightRequest="46"
  IsEnabled="{Binding IsEnabled, Mode=TwoWay}"
  Text="Login"
 TextColor="White"> 
</Button>

** C# code:**

#region Commands
public ICommand LoginCommand
{ get{
       return new RelayCommand(Login);}
}
#endregion

async void Login()
{ if (string.IsNullOrEmpty(Email))
{ await dialogService.ShowMessage( "Error", "You must enter an email");
return;
}
if (string.IsNullOrEmpty(Password))
{
await dialogService.ShowMessage(
"Error",
"You must enter an password");
return;
}
 IsRunning = true;
 IsEnabled = false;
var connection = await apiService.CheckConnection();
     if (connection.IsSuccess){
                IsRunning = false;
                IsEnabled = true;
                await dialogService.ShowMessage("Error", connection.Message);
                return;
            }

var response = await apiService.GetToken("", Email, Password);
     if (response == null){
             IsRunning = false;
             IsEnabled = true;
             await dialogService.ShowMessage(
                    "Error", 
                    "The servic not responding ");
             Password = null;
                return;
            }
            if (response == null || string.IsNullOrEmpty(response.AccessToken))
            {
                IsRunning = false;
                IsEnabled = true;
                await dialogService.ShowMessage("Error", response.ErrorDescription);
                Password = null;
                return;
            }
            await dialogService.ShowMessage("Taran!!", "Welcome! ! !");
              IsRunning = false;
             IsEnabled = true;
        }
        #endregion

Best Answer

  • JGoldbergerJGoldberger Jon Goldberger USXamarin Team, University Xamurai
    edited October 14 Accepted Answer

    @tjr77

    Indent lines of code with 4 spaces (or more to further indent code). Fixed your post so code is shown.

    Anyway, the issue appears to be that you have not set a binding context.

    You can set this in XAML with:

    x:Name="boutonPage 
    BindingContext="{x:Reference Name= boutonPage}"
    

    In 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" 
     xmlns:local="clr-namespace:bouton" x:Class="bouton.boutonPage"
     Title="MainPage"
     x:Name="boutonPage"
     BindingContext="{x:Reference Name= boutonPage}">
        ...
    </ContentPage>
    

    You have to set the x:Name and then refer to that so you refer to the particular instance of boutonPage.

    Or Code:

     using System.Windows.Input;
     using Xamarin.Forms;
    
     namespace bouton
    {
          public partial class boutonPage : ContentPage
        {
               public boutonPage()
            {
                    InitializeComponent();
                    BindingContext = this; // <-------- set the binding context
            }
        ...
    }
    

Answers

  • JohnMillerJohnMiller John Miller USForum Administrator, Xamarin Team Xamurai

    Is this the actual code that appears in your XAML?

    <Button Command="{**Binding LoginCommand **}" 
    

    If so, you'll need to remove the ** marks. I wasn't sure if you did that on the forums for emphasis or not.

  • tjr77tjr77 thierry thierry FRMember ✭✭

    Sorry!I did it for the emphasis forum !

  • tjr77tjr77 thierry thierry FRMember ✭✭
    edited October 14

    to continues just created a short program with only a button with Command behind:
    XAML code:

    <?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:local="clr-namespace:bouton" x:Class="bouton.boutonPage"
     Title="MainPage">
    
          <StackLayout Padding="12" Margin="24">
              <Button
                   Command="{Binding LoginCommand}"
                   BackgroundColor="blue"
                   TextColor="White"
                   HeightRequest="40"
                   Margin="0,0"
                   Text="Login Button">
              </Button>
        </StackLayout>
    </ContentPage>
    

    XAML.CS code:

     using System.Windows.Input;
     using Xamarin.Forms;
    
     namespace bouton
    {
          public partial class boutonPage : ContentPage
        {
               public boutonPage()
            {
                    InitializeComponent();
            }
               public ICommand LoginCommand
            {
                    get
                {
                         return new Command(Login);
                }
            }
              private async void Login()
            {
                   await Navigation.PushAsync(new NavigationPage(new MyPage1()));
            }
        }
    }
    

    It always the same problem with my Command doesn't work
    I under VS2017 for Mac

  • JGoldbergerJGoldberger Jon Goldberger USXamarin Team, University Xamurai
    edited October 14 Accepted Answer

    @tjr77

    Indent lines of code with 4 spaces (or more to further indent code). Fixed your post so code is shown.

    Anyway, the issue appears to be that you have not set a binding context.

    You can set this in XAML with:

    x:Name="boutonPage 
    BindingContext="{x:Reference Name= boutonPage}"
    

    In 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" 
     xmlns:local="clr-namespace:bouton" x:Class="bouton.boutonPage"
     Title="MainPage"
     x:Name="boutonPage"
     BindingContext="{x:Reference Name= boutonPage}">
        ...
    </ContentPage>
    

    You have to set the x:Name and then refer to that so you refer to the particular instance of boutonPage.

    Or Code:

     using System.Windows.Input;
     using Xamarin.Forms;
    
     namespace bouton
    {
          public partial class boutonPage : ContentPage
        {
               public boutonPage()
            {
                    InitializeComponent();
                    BindingContext = this; // <-------- set the binding context
            }
        ...
    }
    
Sign In or Register to comment.