博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Binding Events to Methods in the Silverlight MVVM View Models
阅读量:5841 次
发布时间:2019-06-18

本文共 8366 字,大约阅读时间需要 27 分钟。

原文

Introduction

This article introduces a simple method to bind UI events to the corresponding methods in the MVVM view models in Silverlight applications.

Background

In order to fully conform to the design pattern in Silverlight, we may want to move the event handling functions into the view models. Generally speaking, we have two types of UI components and two types of UI events in Silverlight applications.

  • UI controls like a have a property called . We can create a property in the view model to handle the click event of the button by binding this command property in the view model to the button control's command property in the XAML. For detailed information about the command bindings, you can take a look at my recent article, "".
  • Unfortunately though, most of the UI events like "", "", and "", etc. do not have the corresponding , we are unable to implement the interface to handle these events in the MVVM view models.

This article is to introduce a method to bind these events to the corresponding methods in the view models in Silverlight applications, so we can implement all the event handling functions in the view models, thus minimizing the code-behind files of the XAML views.

SolutionExplorer.JPG

The attached Visual Studio solution is developed in Visual Studio 2010 and Silverlight 4. This solution has two projects.

  • The "EventBindingExample" project is the Silverlight application. In this project, I will be demonstrating how to bind UI events from the XAML views to the methods in the view models.
  • The "EventBindingExampleWeb" project is an ASP.NET project to host the Silverlight application. The Silverlight application is hosted in the "EventBindingExampleTestPage.aspx" page. The "EventBindingExampleWeb" project is the default Silverlight host web project created by the Visual Studio.

To use the event binding method in this article, you need the "". If you have not done so, you need to download the SDK and install it on your computer. In particular, to bind the UI events to the corresponding methods in the view models, you need to add a reference to the "Microsoft.Expression.Interactions.dll", which comes with the Blend SDK. You will also need to add a reference to the "System.Windows.Interactivity.dll".

After adding the references to these two DLLs, binding UI events to the methods in the Silverlight view models becomes very simple. We will now take a look at the implementations of the Silverlight project "EventBindingExample" to find out how the event binding is achieved. We will first take a look at the view model.

The View Model

The "EventBindingExample" project is a simple Silverlight application, which does not have a data model. The following is the view model class "MainPageViewModel" implemented in the "ViewModels\MainPageViewModel.cs" file.

Collapse |
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.ComponentModel;   namespace EventBindingExample.ViewModels {     public class MainPageViewModel : INotifyPropertyChanged     {         // Implementation of INotifyPropertyChanged interface         public event PropertyChangedEventHandler PropertyChanged;         protected void NotifyPropertyChanged(string propertyName)         {             if (PropertyChanged != null)             {                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));             }         }           // Properties         private string displayText;         public string DisplayText         {             get { return displayText; }             private set             {                 displayText = value;                 NotifyPropertyChanged("DisplayText");             }         }           // Public methods         public void WndSizeChanged(object sender, SizeChangedEventArgs e)         {             DisplayText = "Window height: " + e.NewSize.Height.ToString()                 + " Window Width: " + e.NewSize.Width.ToString();         }     } }

Besides implementing the interface, it has a public property and a public method.

  • The property "DisplayText" is a string that will be bound to a in the XAML view to display the size information for the Silverlight window.
  • The public method "WndSizeChanged" will be bound to the "SizeChanged" event of the Silverlight window in the XAML view. This method takes the window size information from the "SizeChangedEventArgs" input parameter and uses it to update the "DisplayText" property.

Now let us take a look at how the view model is bound to the XAML view.

The XAML View "MainPage.xaml"

The XAML view that the view model class "MainPageViewModel" is bound to is implemented in the "MainPage.xaml" file:

Collapse |

The "DisplayText" property of the view model class is bound to a and the "WndSizeChanged" method in the view model is bound to the "SizeChanged" event of the Silverlight window. In order to make the event binding successful, the signature of the method in the view model needs to match exactly the signature required by the event handler of the bounding event.

Now, we finish this demo Silverlight application. Is it so simple? Will the event binding work? Yes, it works. We can now run the application.

Run the Application

Set the "EventBindingExampleWeb" project as the start up project and set the "EventBindingExampleTestPage.aspx" page as the start page, we can debug run the Silverlight application. When the application starts, the "SizeChanged" event of the Silverlight window fires. This event triggers the method "WndSizeChanged" in the view model to update the "DisplayText" property, so the correct size of the window is displayed for you.

RunAppSmall.JPG

If you change the size of the browser window, you can see that the displayed text is changed as you are changing the window size.

RunAppBig.JPG

Conclusion

This article demonstrated that we can bind events to the methods in the view models in Silverlight applications. In concluding this article, we will review the two important points when binding events to the methods in the view models in Silverlight applications.

  • To bind events to the view models in Silverlight applications, you need to use the functionalities provided by the "". In particular, you need to add references to the "Microsoft.Expression.Interactions.dll" and the "System.Windows.Interactivity.dll" in your project.
  • In order to make the event binding successful, the signature of the method in the view model needs to match exactly the signature required by the event handler delegate of the bounding event.

Points of Interest

  • This simple article introduced a method to bind events to the corresponding methods in the MVVM view models in Silverlight applications.
  • The method introduced in this article is simple. But it is significant in developing Silverlight MVVM applications because when combined with , we can almost eliminate all the code-behind methods in the XAML views to better conform to the MVVM pattern.
  • According to , the method introduced in this article may be slightly slower than implementing the event handlers in the code-behind files of the XAML views, because this method uses . The advantage of this method is that it applies to all the UI events fired from all the Silverlight components.
  • The method introduced here is largely from the book "Silverlight 4 in Action" by . If you are interested in Silverlight development, this book should be a good reference for you.
  • Once again, MVVM is a good pattern in developing Silverlight applications. But you still need to make your own judgment on a case by case basis. The best design pattern is the pattern that best fits your application's need, regardless if it is MVVM or not.
  • I hope you like my postings and I hope this article can help you in one way or the other.

History

This is the first revision of this article.

License

This article, along with any associated source code and files, is licensed under

About the Author

United States United States
Member
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.
 

转载地址:http://ppvcx.baihongyu.com/

你可能感兴趣的文章
Nginx 使用 openssl 的自签名证书
查看>>
创业维艰、守成不易
查看>>
PHP环境安装套件:快速安装LAMP环境
查看>>
CSS3
查看>>
ul下的li浮动,如何是ul有li的高度
查看>>
C++ primer plus
查看>>
python mysqlDB
查看>>
UVALive 3942 Remember the Word Tire+DP
查看>>
从微软的DBML文件中我们能学到什么(它告诉了我们什么是微软的重中之重)~目录...
查看>>
被需求搞的一塌糊涂,怎么办?
查看>>
c_数据结构_队的实现
查看>>
jquery 选择器总结
查看>>
Qt设置背景图片
查看>>
【阿里云文档】常用文档整理
查看>>
java中的Volatile关键字
查看>>
前端自定义图标
查看>>
实验二
查看>>
独立开发一个云(PaaS)的核心要素, Go, Go, Go!!!
查看>>
网站文章如何能自动判定是抄袭?一种算法和实践架构剖析
查看>>
【OpenCV学习】滚动条
查看>>