Reputation: 3427
I am quite new to MVVM, so sorry for probably simple question. However, I can not understand which mechanism from MVVVM (I am using MVVMLight if that is of any consequence) to use in order to program the simple following scenario:
I have textbox TB, where user can fill in URL. Than I have a button B and webview WV. If user clicks on button, the app should take the text from TB and display it in the WV.
I knwo that I can create a property in viewmodel and bound it to TB.Text. I understand probably also that I should create command which will be boudn from button B, but what should I do in the command. How I can call WV.navigate(url), when I do not have reference to WV. Should this be solved by something, which I did not grasp correctly like behaviours? What is the best way to do this?
Upvotes: 1
Views: 3993
Reputation: 11868
You should use the messenger pattern for this problem:
The idea is that the view can register for specific message classes (in this case for example an own NavigateToUriMessage
class) and the view model can send an instance of this message class to whoever listens to the message type. In the command implementation you simply send this message, the view receives the message and changes the URI of the web view.
BTW: The idea of this messenger pattern is that you can better write Unit Tests and use the view model for other platforms (where the reaction to the message may differ).
Upvotes: 3
Reputation: 11868
Another way is to create an attached property for the WebView
class where you can bind an Uri
property to. The attached property calls Navigate
when the bound value changes.
Check out this blog:
Upvotes: 2