WPF中的MVVM-UserControls和GMap.NET [英] MVVM in WPF - UserControls and GMap.NET

查看:203
本文介绍了WPF中的MVVM-UserControls和GMap.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几乎所有的教程并检查了堆栈问题,但是我仍然不知道,也不了解如何在MVVM中管理WPF UserControl,特别是那些由GMap.NET之类的人提供的控件.

I have read almost all the tutorials and checked stack questions however I still do not know and do not understand how to manage WPF UserControls in MVVM, specially those provided by others like GMap.NET.

我试图在我的应用程序中遵循MVVM模式,但是我不知道如何在其中初始化和管理GMap.NET WPF控件.在标准方法中,它需要这样的管理:

I am trying to follow MVVM pattern in my app, however I have no idea how to initialize and manage GMap.NET WPF control in it. In standard approach it requires management like this:

gmap.MapProvider = GMap.NET.MapProviders.GoogleMapProvider.Instance;
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly;
gmap.DragButton = MouseButton.Left;
gmap.SetPositionByKeywords("Paris, France");
gmap.ShowCenter = false;

如何在不停止MVVM的情况下执行此操作?是否有可能?我将不胜感激.

How can I do this without braking MVVM? Is it possible? I would be grateful for any help.

推荐答案

如注释中所建议,任何仅初始化视图的代码都可以放在后面的代码中.

As suggested in comments, any code that just initializes the view can be placed in code behind.

如果可能,应将视图模型中的属性绑定到视图.这是首选方式,应该减少大多数用例.

If possible, properties from the view model should be bound the view. That is the prefered way and should beat down the most use cases.

但是,某些控件很复杂,并且具有高级" API,该API不仅包括依赖项属性,还包括方法和事件.

However, some controls are complex and have an "advanced" API that consists not only of dependency properties, but also of methods and event.

如果需要从视图模型中调用视图方法",则基于事件的方法可以正常工作.由于视图已知视图模型,因此它可以订阅视图模型上的事件(例如,在DataContextChanged事件中). (另请参见该答案) 示例:您要从视图模型中调用方法gmap.SetPositionByKeywords.只需给视图模型一个事件RequestSetPositionByKeyWords,让该视图订阅该方法,并在事件引发后调用gmap.SetPositionByKeywords.参数和返回值可以通过事件args进行传输.这是尊重MVVM原则的不错方法.

If it is required to call "view methods" from the view model, the event-based approch works fine. Becuase the view knowns the view model, it can subscribe to events on the view model (e.g. in the DataContextChanged event). (see also that answer) Example: You want to call the method gmap.SetPositionByKeywords from the view model. Just give the view model an event RequestSetPositionByKeyWords, let the view subscibe to that method and call gmap.SetPositionByKeywords if the event has been raised. Parameters and return values can be transfered by event args. That is a nice approch which respects the MVVM principals.

这篇关于WPF中的MVVM-UserControls和GMap.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆