Search code examples
c#mvvmxamarin.formsreactiveui

Xamarin.Forms - Pass a value from View to ViewModel


Anyone know how to pass a random string value to the viewmodel from within the view's code-behind in Xamarin.Forms?

  1. The string value doesn't come from any of the controls being displayed to the user.

  2. The value is not available until well after the page is displayed to the user.

  3. The value is only available to me to consume from within the view, not the viewmodel.


Solution

  • You can do it by accessing your ViewModel through the BindingContext of your View like so:

    ((ViewModelType)this.BindingContext).SomeProperty = "someRandomString"