在Windows MVVM通用的应用程序 [英] Universal app in windows MVVM

查看:120
本文介绍了在Windows MVVM通用的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始在通用的应用程序开发。我已经在Windows 8商店应用程序开发的应用程序,并还在开发的Windows Phone 8和Windows Phone 8.1(SilverLight的)应用程序。问题是有关在Windows中通用的应用程序和在应用程序中创建单一的用户界面。共享文件夹。

I have just started to develop in universal app. I have developed app in Windows 8 store apps and also developing Windows Phone 8 and Windows Phone 8.1 (SilverLight) Apps. Question is related to universal app in Windows and Single UI which is created in App. Share folder.

1 我在 MVVM新。我开发的Windows phone 8和WP8.1应用程序在正常的方式。但不能与MVVM。我搜索了很多,但没有得到任何的例子能更好地MVVM的理解。我知道什么是MVVM。这是同样的Asp.net MVC的。

1. I am new in MVVM. I have developed Windows phone 8 and WP8.1 app in normal way. But not with MVVM. I have search a lot but did not get any example which can make better understand of MVVM. I know what is MVVM. It is similarly of MVC of Asp.net.

MVVM

< STRONG>型号:描述数据

视图模型:在简单的文字Model和View之间的桥梁。

View-Model : In simple word a bridge between Model and View.

查看:一个简单的XAML页面或用户界面

View : A simple xaml page or user interface.

哪种方法更好或最佳做代码在Windows Phone 8的MVVM还是正常的方式?

Which way is better or best to do code in Windows Phone 8 MVVM or Normal way?

普通的方式是指不使用MVVM。

Normal way means without using MVVM.

参考:单独的用户界面和应用程序使用的逻辑模型 - 视图 - 视图模型模式

推荐答案

这是一个广泛的问题,在一个岗位回答。我会提供一些事情去思考和研究

This is a broad question to answer in a single post. I'll provide a couple of things to think about and research.

(注意:在这个职位所有代码都是从内存中腾出手它可能不是100%的语法。正确的。)

(Note: all code in this post is free hand from memory. It may not be 100% syntactically correct.)

您是在MVVM的定义大部分是正确的。 MVVM和MVC之间我主要区别在于,绑定用于连接视图查看模型(控制器)。这就是语义,但并不必先挂了就可以了。

You are for the most part correct in your definition of MVVM. The main difference to me between MVVM and MVC is that binding is used to connect the View to the View Model (Controller). That is semantics though and don't need to get hung up on it.

如果你是WP 7,WP 8,WP8.1,Siverlight没关系或通用应用程序。这可能是最容易下手为WPF MVVM时被引入,这是更快的运行你的应用程序,因为它不会在模拟器中运行即是。

It doesn't matter if you are WP 7, WP 8, WP8.1, Siverlight or Universal App. It may be easiest to start with WPF as that is when MVVM was introduced and it is faster to run your app as it doesn't run in an emulator.

首先是开始使用一个非常简单的例子。

First is to get started with a very basic example.

1)创建文件夹视图和的ViewModels。不严格必要的,但你会希望他们。

1) Create folders "Views" and "ViewModels". Strictly not necessary but you will want them.

2)创建一个TextBlock一个空白页。这是后面的代码XAML文件。在浏览文件夹中创建并命名为MyFirstPage。把一个文本块这样的:

2) Create a blank page with a TextBlock. This is the xaml file with the code behind. Create it in the Views folder and name it MyFirstPage. Put a text block on it like this:

<TextBlock Text="The Title of my App" />



3)运行应用程序,并确保该文本显示。

3) Run the app and make sure this text is showing.

4)创建一个视图模型类。这是一个基本的类文件。在的ViewModels文件夹中创建并命名为MyFirstViewModel。

4) Create a ViewModel class. This is a basic class file. Create it in the ViewModels folder and name it MyFirstViewModel.

5)连接页面(视图)视图模型。这里是我的代码屁股的样子,并在大多数情况下这是怎么我在其中。设置的DataContext到视图模型的关键是建立具有约束力。还有许多其他的框架存在,使这种神奇的,但是这是会发生什么。这是在我看来最好的地方开始。

5) Connect Page(View) to ViewModel. Here is what my code behinds look like and for the most part this is all I have in them. Setting the DataContext to the view model is the key to setting up the binding. There are many other frameworks out there that make this magic, but this is what happens. This is the best place to start in my opinion.

public class MyFirstPage : Page
{
    private MyFirstViewModel _viewModel = new MyFirstViewModel();

    public MyFirstPage()
    {
        this.Initialize???
        this.DataContext = _viewModel;
    }



6)添加标题属性您的视图模型和现在只是有它返回一个硬编码值。

6) Add a Title Property to your View Model and for now just have it return a hard coded value.

public string Title { get { return "The Title of my App (set from View Model)"; } }



7)更新的TextBlock上的查看使用绑定

7) Update TextBlock on View to use Binding

<TextBlock Text="{Binding Title}" />



8)运行的应用程序进行测试,它的作品。

8) Run the app to test that it works.

所以这是挂钩视图模型到视图,看到了绑定工作的基本知识

So that is the basics to hook up a view model to a view and see the binding work.

接下来的学习。


  1. 双向绑定:如果你有价值观在文本框的UI被设置,您将需要更新的结合看起来像{绑定名字,模式=双向}作为一个例子,如果你想输入名字

  1. Two Way Binding: If you have values being set on the UI in TextBox you will need to update the binding to look like {Binding FirstName, Mode=TwoWay} as an example if you want to enter a first name.

观测属性:你会发现另一个问题是,当您查看模型的逻辑改变了绑定属性的值不会在UI上显示的值。你会拉你的头发,想知道什么是错误的,但它是非常简单的。用户界面需要通知进行更新。你改变了潜在价值,但UI不知道更新。因此,对于像名字属性,你将需要实现你的视图模型,并在你的属性调用OnPropertyChanged(名字)的制定者INotifyPropertyChanged的。有很多的例子,在那里,描述这个

Observable Properties: Another problem you will find is that when you view model logic changes the values of the bound properties the values won't display on the UI. You will pull your hair out wondering what is wrong but it is really simple. The UI needs to be notified to update. You changed the underlying value but the UI has no idea to update. So for properties like FirstName you will need to implement INotifyPropertyChanged on your ViewModel and in the setter of your property call OnPropertyChanged("FirstName"). There are lots of examples out there that describe this.

ObservableCollections :如果你有一个项目列表类似观测属性的在视图模型得到调整,认为将需要通知该列表已经改变。做到这一点的方法是使物业一个ObservableCollection。此外,大量的例子。尖端我只是实现这些属性一个getter。你想要么在属性的getter构造函数或延迟加载,一旦创建集合。如果你创建的ObservableCollection的新实例链接到UI会破产,你将不得不调用OnPropertyChanged这这确实是没有必要的,如果你只是使用的ObservableCollection的一个实例,并和并从中取出物品。你会看到什么,我用它打了一下之后的意思。刚刚重新阅读这又

ObservableCollections: Similar to Observable Properties if you have a list of items that get adjusted in the view model, the view will need to be notified that the list has changed. The way to do this is to make the property an ObservableCollection. Again, lots of examples. The tip I have is only implement a getter for these properties. You want to either create the collection once in the constructor or lazy load in the getter of the property. If you ever create a new instance of the ObservableCollection the link to the UI will get broke and you will have to call OnPropertyChanged for this which really is not necessary if you just use a single instance of ObservableCollection and and and removed items from it. You'll see what I mean after playing with it a bit. Just re-read this again.

转换:现在我们正在向一个新的水平,但要保持你的代码背后的清洁你将利用转换器和继电器的命令。一个最常见的转换器是BooleanToVisibilityConverter。这将有助于控制视图组件根据您的视图模型一个布尔值的可见性。同样,你将不得不研究这个

Converters: Now we are moving to the next level but to keep your code behind clean you will leverage converters and relay commands. A most common converter is BooleanToVisibilityConverter. This will help control the Visibility of a view component based on a boolean value on your View Model. Again, you will have to research this.

命令中继:你需要命令中继保持落后清洁你的代码就像转换器。中继命令基本上是为click事件绑定。而不必在代码中Click事件处理程序的背后,你会在你的视图模型中继命令实现,比如一个按钮命令将绑定到你的视图模型RelayCommand属性。

Relay Commands: Like converters you need Relay Commands to keep your code behind clean. A relay command is basically a binding for a click event. Instead of having a click event handler in the code behind you will have a Relay Command implemented in your ViewModel and for instance a Button Command will bind to the RelayCommand property on your View Model.

在你的研究和熟悉这些项目,你将是一个良好的开端。

Once you research and are familiar with these items you will be off to a good start.

有真的很难在某些情况下,为了避免后面的代码,但我发现的是,我已经能够找到大多数问题的解决方案,但是,它有时需要创造力。

It is really hard in some cases to avoid the code behind, but what I have found is that I have been able to find solutions for most problems, however, it sometimes requires creativity.

一旦最后的评论:
我在创造一个干净的严格视图模型的目标是,让我可以在外形尺寸(手机和平板电脑)重用。这是可能的,但一旦你更加努力的问题得到深层的是比较困难的。但是,这里的关键是,你必须为你的ViewModels住。一个独立的库项目我所有的解决方案具有的Windows Phone 8.1项目,在Windows 8.1(店)项目,以及便携类库项目。该文件夹的ViewModels进入库项目一起将所有的其他代码是共享的。为了使一切工作,你可能需要使用控制反转但这是另一篇文章的话题。

Once last comment: My goal in creating a strict clean ViewModel is so that I can reuse it across form factors (Phone and Tablet). This is possible but is more difficult once you get deep in to harder problems. However, the key here is that you have a separate Lib project for your ViewModels to live in. All my solutions have Windows Phone 8.1 Project, a Windows 8.1 (Store) Project, and a Portable class Lib project. The ViewModels folder goes into the Lib project along will all other code that is shareable. To make everything work you may have to use Inversion of Control but that is a topic for another post.

好运气,玩得开心,

汤姆

这篇关于在Windows MVVM通用的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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