简体   繁体   English

Windows Phone的MVVM设计模式

[英]MVVM Design Pattern for Windows Phone

I am writing a Windows Phone application and planned to use some MVVM idea in it. 我正在编写Windows Phone应用程序,并计划在其中使用一些MVVM想法。
I know that with command binding I can easily separate UI and logic, but the problem is I don't know how I can get access to the controls properties, for example: 我知道使用命令绑定可以轻松地将UI和逻辑分开,但是问题是我不知道如何访问控件属性,例如:
I have a map control on the main page, for which I want to show a push pin at the place user taps at. 我在主页上有一个地图控件,为此,我想在用户点击的位置显示一个图钉。 But with MVVM I don't think there is anyway I can do that. 但是使用MVVM,我认为我无法做到这一点。 Means I can not access to the maps properties and methods to do things like that, is it right? 意味着我无法访问maps属性和方法来执行类似的操作,对吗?

If anyone has experience with MVVM please share your idea about how to use control's properties and methods to perform tasks. 如果任何人都有使用MVVM的经验,请分享您有关如何使用控件的属性和方法执行任务的想法。

(PS I dont want to use MVVM Light or any toolkit for this application) (PS我不想为此应用程序使用MVVM Light或任何工具包)

Thank you 谢谢

What you're trying to do - binding a to a property which determines where to show a pin on the map - is a pretty specific use case, which is why the map control does not expose a property for this. 您正在尝试做的-将a绑定到一个属性,该属性确定在地图上的哪个位置显示图钉-是一个非常特定的用例,这就是为什么map控件不会为此公开一个属性。

If you want to follow the MVVM pattern I would recommend creating a user control / custom control that wraps the map control and exposes a dependency property to which you can bind to. 如果要遵循MVVM模式,建议您创建一个用户控件/自定义控件,该控件包装地图控件并公开一个可以绑定到的依赖项属性。 When this property is changed you check if there's a pin added to the map and if not, add one; 更改此属性后,您检查地图上是否添加了图钉,如果没有,则添加图钉。 otherwise move it to the correct position. 否则将其移动到正确的位置。

Bottom line is you'll need to create some custom control and expose the property/properties to bind to if you want to implement the solution following the MVVM pattern. 底线是,如果您想按照MVVM模式实施解决方案,则需要创建一些自定义控件并公开要绑定的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM