recently I started working with PRISM in Silverlight. Let say I have a View that contains a special Map control that has methods that I currently use in my code behind, which I would like to move to the ViewModel. For example:
private void DoSomething()
{
Map.ZoomTo(Layer.InitialExtent);
}
Later on I will need to use more methods, like:
Map.PanTo(...);
Map.MapToScreen(...);
I know that for properties I should use Binding. But what is the practice for methods? (I certainly don't want to leave it in the开发者_StackOverflow社区 code behind).
Can someone tell me how to access the Controls methods from the ViewModel?
Leave it in the code behind. It sounds like what you are doing is purely visual. MVVM is a way for the View to express data exposed from the ViewModel through bindings etc... If there is no way to change data in the ViewModel to express what you are trying to do, then leave it in the code behind. MVVM doesn't mean no code behind.
I agree with brianstewey.
MVVM is not about deleting the code-behind. the ViewModel is like a bridge between the view and the model but if you need to do things that concerns only to the view, that code should be in the view's code-behind. Things like that for example.
Just think:
You put that map stuff on your VM and tomorrow you decide that you want another view for the same viewmodel, doing the same thing as your existing one but instead of using that special map, your are going to use another control. You'll figure out that you have view stuff on your VM and you don't need that on your new View because that belongs to the other view.
In short, when something is just view stuff, use the code-behind.
精彩评论