In this chapter, we will be looking at an important technique in Silverlight's software development, the use of View Models.
View Models are intended to resolve certain problems that crop up repeatedly when emerging user interface software. Perhaps the most significant one is that user interface code is often problematic to indivisibly test, particularly with automated unit tests. There are also code quality problems that can affect the ongoing elasticity and maintainability of your code.
To escape the problems caused by placing application logic in the code behind or XAML, it is superlative to use a method known as separated presentation. Having XAML and code behind with the least required for working with user interface objects directly, a user interface classes also contain code for complex interaction behaviors, application logic, and everything else as shown below on left side.
Vital features of Separated Presentation −
View Model is an instance of the separated presentation approach, but let us be clear about precisely what kind of thing we have in each layer. There are three layers −
This is a classic object model including of ordinary C# classes that has no direct connection with the user interface.
You would normally expect your Model codes to be able to compile without references to any user interface libraries. In fact, you would probably be able to take the exact same source code and compile it into a Silverlight application, an ordinary .NET Console application, or even server-side web code.
The types in the Model should signify the ideas your application works with.
A View is usually a UserControl, it might be your MainPage, or it might just be some part of your page.
In greatest Silverlight applications, it is a good idea to split your user interface up into small pieces defining a UserControl, or View for each piece.
Silverlight applications are not sole in this respect. Somewhat that is clearly Silverlight specific is the View. The more fine-grained your user interface is, the better things tend to be. Not only are you less probable to trip over other developers working on the same files, keeping things small and simple naturally discourages the shortcuts that lead to spaghetti-like code.
For instance, it is very mutual to define a View to represent an individual item in a List.
Lastly, for each View, you write a ViewModel. So, this is one of the important features of a ViewModel class.
It is to serve a specific View. The ViewModel is specialized for a particular way of offering things, such as a specific data item as it appears in Lists.
This is why it is called a ViewModel; it become accustomed the underlying Model particularly for a particular View. Like the Model, the ViewModel is also an ordinary C# class. It does not need to derive from any particular type.
As it occurs, some developers find it suitable to put some common functionality into a base ViewModel class, but the pattern does not demand that. In particular, your ViewModel does not derive from any Silverlight specific type. However, unlike the model, it can use Silverlight types in its properties.
For instance, your ViewModel might select to make certain parts of your user interface visible only under certain conditions, and so you might provide a property of type System.Windows.Visibility, which is the type Silverlight elements use for their Visibility property. This makes it possible to bind the visibility of an element, such as a panel, directly to the ViewModel.
Let us look at a simple example in which we will be using Model-View-ViewModel (MVVM) approach.
Step 1 − Create a new Silverlight Application project SilverlightMVVMDemo.
Step 2 − Add the three folders (Model, ViewModel, and Views) into your project as shown below.
Step 3 − Add a StudentModel class in the Model folder and paste the below code in that class.
Step 4 − Add another StudentViewModel class into ViewModel folder and paste the following code.
Step 5 − Add Silverlight User Control by right-clicking on Views folder and Select Add New Item….
Step 6 − Click Add. Now you will see the XAML file. Add the following code into StudentView.xaml file, which contains different UI elements.
Step 7 − Now add the StudentView into your MainPage.xaml file as shown below.
Step 8 − Here is the implementation of Loaded event in the MainPage.xaml.cs file, which will update the View from the ViewModel.
Step 9 − When the above code is compiled and executed, you will see the following output on you webpage.
MVVM deals the next benefits −
For simple UIs, MVVM can be overkill. Debugging would be a bit difficult when we have complex data bindings.
Microsoft Silverlight Related Interview Questions
|Microsoft Excel Interview Questions||Microsoft Word Interview Questions|
|Microsoft Powerpoint Interview Questions||Microsoft Dynamics CRM Interview Questions|
|Microsoft Silverlight Interview Questions||Microsoft Outlook Interview Questions|
|Microsoft Entity Framework Interview Questions||Microsoft Azure Interview Questions|
|Microsoft Project Interview Questions||Microsoft Dynamics NAV Interview Questions|
|Microsoft Interview Questions||Microsoft Office 365 Interview Questions|
|Microsoft Sharepoint 2013 Interview Questions||Microsoft Power Bi Interview Questions|
Microsoft Silverlight Related Practice Tests
|Microsoft Excel Practice Tests||Microsoft Word Practice Tests|
|Microsoft Powerpoint Practice Tests||Microsoft Dynamics CRM Practice Tests|
|Microsoft Silverlight Practice Tests||Microsoft Outlook Practice Tests|
|Microsoft Entity Framework Practice Tests||Microsoft Azure Practice Tests|
|Microsoft Dynamics NAV Practice Tests|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.