MVVM Light:在哪里从模型实例化类以及如何更新/访问它?
现在,我正在开发我的第一个 WP7 应用程序,并遇到了一些问题,尽管阅读了我在网上找到的内容,但我仍无法回答这些问题。请考虑一个具有主页、参数页面和结果页面的应用程序。在参数页面中,用户可以在各个文本框中输入或更新数字。点击后退按钮会将用户带回主页,其中有一个名为“计算”的按钮。点击该按钮应该获取数据,用它执行计算并将用户带到显示结果的网格的结果页面。
在名为 Calculator.cs 的文件中,我在名为 Models 的文件夹内有一个名为 Calculator 的类。 ViewModels 文件夹中还有 MainViewModel.cs、ParametersViewModel.cs 和 ResultsViewModel.cs 文件以及相应的 MainPage.xaml,以及名为 Views 的文件夹中的Parameters.xaml 和 Results.xaml。我假设所有数据都将在 Calculator 类的实例中进行操作,然后返回结果集并将其定向到 Results.xaml。我只是不知道在哪里实例化 Calculator 类、传递数据,然后检索结果。我还有些困惑,计算完成后如何触发自动导航到“结果”页面。
任何对此的帮助将不胜感激。
更新: 传递在 WP7 Silverlight 应用程序中导航时将复杂对象转换为页面有一些关于同一主题的更多信息。我可以进入 App.xaml.cs 并添加如下内容:
public class Foobar
{
public string barfoo = "hah!";
}
public static Foobar myfoob = new Foobar();
然后从 ViewModel 页面访问它,例如 AboutViewModel.cs,如下所示:
public AboutViewModel()
{
string goo = App.myfoob.barfoo;
}
但此时我仍然不确定可能会产生什么不可预见的影响。此时我将解决序列化/逻辑删除问题,看看使用此方法或跨页面使用相同的 DataContext 会发生什么情况。否则,上面链接中的海报之一提到序列化参数并在页面之间传递它们。我担心的是,HTTP GET 是否有字符限制。似乎有:Silverlight 中的 URI 限制
Right now, I'm working on my first WP7 app and have run into some questions, which I haven't been able to answer despite reading what I could find online. Please consider an app that has a main page, a parameters page and a results page. In the parameters page, the user can enter or update numbers in various textboxes. Hitting the back button takes the user back to the main page, where there is a button called "Calculate". Hitting that button should take the data, perform a calculation with it and take the user to the results page presenting a grid with the results.
In a file called Calculator.cs I have a class called Calculator inside a folder called Models. I also have my MainViewModel.cs, ParametersViewModel.cs, and ResultsViewModel.cs files inside the ViewModels folder and the corresponding MainPage.xaml, along with Parameters.xaml and Results.xaml inside a folder called Views. I'm assuming that all the data will be manipulated within the instance of the Calculator class and then a results set will be returned and directed to Results.xaml. I'm just at a loss as to where to instantiate the Calculator class, pass it data, then retrieve the results. I'm also somewhat puzzled how I will trigger the automatic navigation to the Results page when the calculation is done.
Any help with this would be greatly appreciated.
UPDATE: Passing a complex object to a page while navigating in a WP7 Silverlight application has some more info on the same subject. I can go into App.xaml.cs and add something like this:
public class Foobar
{
public string barfoo = "hah!";
}
public static Foobar myfoob = new Foobar();
Then access it from a ViewModel page, e.g. AboutViewModel.cs, like this:
public AboutViewModel()
{
string goo = App.myfoob.barfoo;
}
But at this point I'm still uncertain what unforseen effects that might have. I'm going to tackle serialization/tombstoning at this point to see what happens with either this approach or by using the same DataContext across pages. Otherwise, one of the posters in the link above mentioned serializing the params and passing them between pages. My concern there would be whether or not there is a character limit as with HTTP GET. Seems there is: URI Limits in Silverlight
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然有很多可能的设计 - 并且其中很多在不同方面都是正确的!
我可能会使用以下一个:
其他解决方案肯定是可用的 - 将有兴趣阅读其他人的建议和偏好。
顺便说一句,在结果页面上需要注意的一件事是墓碑 - 可能是一个有趣的挑战!
There are of course lots of possible designs - and lots of them are correct in different ways!
Here's one I might use:
Other solutions are definitely available - will be interested to read what other people suggest and prefer.
As an aside, one thing to watch out for on your Results page is tombstoning - could be an interesting challenge!