Question

I am using Xamarin to develop an App for Android in C# using MvvmCross.

The app compiles and runs fine when I am using one of my View Models which is bind to a seekbar, but doesn't with the View Models that I want to bind with a button. All my View models have a very similar structure (pretty much identical besides the names).

A sample of my setup is presented below:

My View Model looks something like:

 public interface ISomething
    {
       int MethodSomething(int i);
    }

   public class ChangeSomethingViewModel : MvxViewModel
    {
        private readonly ISomething _somethign;
    public ChangeSomethingViewModel(ISomething something)
    {
        _something = something;
    }
    public override void Start()
    {
        Terminate_Something = "hello";
        base.Start();
    }
.
.
.
        public string terminate;
        public string Terminate_Something
        {
            get { return terminate; }
            set { terminate= "pressed"; RaisePropertyChanged(() => Terminate_Something); }
        }

I then have another file with MethodSomething(int i)

public class ChangeSomething : ISystem
    {
    public int MethodSomething(int i)
    {
        .
        .
        .
    }
     }

In setup.cs

        protected override IMvxApplication CreateApp()
        {
            Mvx.RegisterSingleton<IMvxAppStart>(new MvxAppStart<ChangeSomethingViewModel>());
            return new App();
        }
protected override void InitializeFirstChance()
        {
            Mvx.RegisterSingleton<ISomething>(new ChangeSomething());
            base.InitializeFirstChance();
        }

In my Main.axml file I have a button

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Click Terminate_Something" />

When I run the app Terminate_Something is set and displayed in the button, as I would expect. However, if I place a breakpoint in Terminate_Something and click on the button no call to Terminate_Something is made.

I should probably mention that I can see in the log the following every time I touch the button.

04-17 12:57:14.070 D/GestureDetector( 5928): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 7 mFalseSizeCnt:0

I can't quite understand what can be the problem as my other view model works great linking to a slider. When I press on the slider (seekbar) moves and the calls Set, the corresponding log is

04-17 12:57:45.060 D/ProgressBar( 5928): setProgress = 43, fromUser = true
04-17 12:57:45.060 D/ProgressBar( 5928): mProgress = 50mIndeterminate = false, mMin = 0, mMax = 70

Thank you in advance

Was it helpful?

Solution

It looks like the problem is because you are not binding to an ICommand. Also, I might be missing it somewhere, but you should also specify in Terminate_Something MethodSomething, no?

This should fix it: In your View Model change your Terminate_Something property to

 private MvxCommand terminate;
   public System.Windows.Input.ICommand Terminate_Something
   {
       get
       {
           terminate = terminate ?? new MvxCommand(MethodSomething);
           return terminate;
       }
   }

Since you mentioned that the SeekBar is working didn't you do something like?

set { terminate= "pressed"; RaisePropertyChanged(() => Terminate_Something); MethodSomething();}

In any case Stuart has a video pretty much on this very thing in N=5

EDIT:

Obviously, you can't set terminate to "hello" in start()... depending on what you are trying to do with the button you can bind the text "hello" of the button to a different property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top