Search code examples
c#xamarinmvvmfreshmvvm

What is the best practice to access Page items attributes in Freshmvvm?


Ok, so I'm very new to Freshmvvm and MVVM pattern.

I followed some tutorials and read everything I could find on freshmvvm but when I started coding I hit a wall.

How do I change an item, like a <Label x:myLabel /> text property without binding it to a variable in the PageModel? Is it possible? On pure Forms I can use the code behind to do something like myLabel.Text = "Hello World". But in Freshmvvm I don't have a reference to my Page items in my PageModel.

So if I'm to use the code behind files, then I'll need a reference for the PageModel in the code behind files and vice-versa. How to do it?

Or should I just forget about the code behind files and do all the coding in the PageModel? If so, how do I get the Page instance to reference my item 'x:Name"s?

Or must I bind every item attribute I want to change to a variable in the PageModel? I find this option really restrictive.

Thanks.


Solution

  • The whole idea of MVVM is to not directly access the view (page) from the view model (page model) that is why there is not a reference to the page from the page model. If you need to reference the page model from the page you can cast the page's binding context to your page model. I think it is better to bind the textbox's text to a property and update that.

    var pm = this.BindingContext as MyPageModel