question

Jeheuira-6454 avatar image
0 Votes"
Jeheuira-6454 asked JarvanZhang-MSFT commented

ICommand property won't work on Button relocation

I am trying to use Xamarin Binding property for the first time.
But when I tried using the ICommand interface as shown in Xamarin documentation, I faced some issues, which I believe due to synchronization.

I have a TrolleyMock class, which sends random numbers every few seconds for x,y,z locations.
And there is the LocationProvider class, in which all the visual components are initialized.
I am trying to create a TrolleyMock instance and bind its xPosition variable to my button’s TranslationX property.
But when I do that, it does not move(nothing happens actually).

I did the same thing with a slider and it worked.


Did you face such issues? What would you recommend? I am sorry in advance if it is a simple question, but I couldn’t find a solution till now.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

Hi, Jeheuira-6454. How did you set the binding for the property? If you use a model class to do this, please make sure the model class inherits from the INotifyPropertyChanged interface so that it can raise the PropertyChanged event.

<Label Text="testing for translationX" TranslationX="{Binding TheValue}"/>

//model class
public class TestModel : INotifyPropertyChanged
{
    private int theValue;
    public int TheValue
    {
        get
        {
            return theValue;
        }
        set
        {
            if (theValue != value)
            {
                theValue = value;
                NotifyPropertyChanged();
            }
        }
    }
    protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the response. It is still not working. When I do

 <Label Text="testing for translationX" TranslationX="{Binding TheValue}"/>

it gives a warning saying "No Data Context found for Binding 'TheValue'."

I checked this warning, it has something to do with how to do the binding but still could not figure it out.

0 Votes 0 ·

Did you set the BindingContext for the page? For example, I set the bindingContext with the 'TestModel' class for the page to bind the data.

public partial class Page1 : ContentPage
{   
    public Page1()
    {
        InitializeComponent();

        BindingContext = new TestModel();
    }
}
0 Votes 0 ·

To be more clear, it is not working when I am trying to use another variable like this:

 public class TestModel : INotifyPropertyChanged
     {
         private int theValue;
         private LocationTracker locT  = new LocationTracker(ip);
         public int TheValue
         {
             get
             {
                   theValue = locT.getXPosition();
                   return theValue;
             }
             set
             {
                 if (theValue != value)
                 {
                     theValue = locT.getXPosition();
                     NotifyPropertyChanged();
                 }
             }
         }

The LocationTracker is updating X position constantly and when I call the getXLocation() it sends the current X position. And I just want to relocate my button according to that.




0 Votes 0 ·
Show more comments