




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
计算机科学与工程学院 现代软件技术实验报告 武 汉 工 程 大 学计算机科学与工程学院现代软件技术实验报告2专业班级实验时间学生学号实验地点学生姓名指导教师实验项目Using MVVM pattern to realize a customer management system实验类别基本型实验实验学时6实验目的及要求实验要求:Windows 7或以上操作系统、Visual Studio .Net 2010或以上版本的应用程序,SQL Server 2005数据库或以上版本、Windows Phone 7 Toolkit 7.0或以上版本。学生应按指导教师的要求独立完成实验,并按要求撰写实验报告(应包含实验核心程序、实验过程和结果中的关键图片)。成 绩 评 定 表类 别评 分 标 准分值得分合 计上机表现按时出勤、遵守纪律认真完成各项实验内容30分报告质量程序代码规范、功能正确填写内容完整、体现收获70分说明: 评阅教师: 日 期: 年 月 日实 验 内 容(1) Creating the Data ModelThe model is a single class named Accomplishment. The Accomplishment class implements the INotifyPropertyChanged interface and the PropertyChanged event. This allows the class to notify its views when a property value changes, and then the views can update the user interface based on that change. In this application, the user can only change the Count and Completed properties, so you only need to raise the PropertyChanged event in those properties. You will test the data binding in the final procedure.(2) Creating the ViewModelIn this procedure, you create the ViewModel, which is the link between the Model and the View. The ViewModel is a collection that contains objects from the Model, in this case Accomplishment objects. The ViewModel uses an ObservableCollection. This allows it to notify its views when items in the collection change and the views can update the user interface based on that change.The ViewModel contains the code to get the collection of accomplishments. First, you check the isolated storage to see if the accomplishments are already saved. If yes, you use isolated storage to populate the collection. If not, you populate the collection with default accomplishments. In a later procedure, you will add the code to save the accomplishments to isolated storage.(3) Creating the First ViewIn this procedure, you create a view to display the item accomplishments. In a later procedure, you will display the view on a page and connect it to the ViewModel by setting its data context. Note that there is no code behind this view.The view is a user control that contains a ListBox bound to the data. Each item in the ListBox has the following three columns.(4) Creating the Main Application PageIn this procedure, you create the main application page. The main page contains the two views that you created previously. The first view contains the items and the second view contains the levels. In the code, first you create a new instance of the ViewModel and use it to get the data. Then you connect each View to the ViewModel, choosing the data from the ViewModel that you want to see in the View. In a later procedure, you will add the code to maintain the page state when the user navigates away from your application and back.(5) Maintaining Page StateIn this procedure, you add the code to maintain the page state. If the user navigates away from your application and then back, you want to maintain the page state. If the user starts a new instance of the application from the application list, you do not want to maintain the page state. For more information, see Execution Model for Windows Phone.First, you create a utility class that has one property named IsLaunching. You use the property to track if the user has started a new instance of the application. The only state that you need to maintain in this application is the ViewModel. Silverlight for Windows Phone provides a page state object, and you can store the entire ViewModel in the state as one object. You do not need to store the items from the ViewModel collection individually. When the user returns to the application, you can retrieve the ViewModel from the state and bind the views to it. You will test the page state in the final procedure.(6) Saving Data to Isolated StorageIn this procedure, you will add the code to save the application data to isolated storage. In this application, you will save the data in the isolated storage settings dictionary, not a text file. For more information about the types of isolated storage, see Isolated Storage Overview for Windows Phone. For more information about the local settings dictionary, see the IsolatedStorageSettings class. You will test the isolated storage in the final procedure.The data that you save to isolated storage are the Accomplishment objects, which are data-bound. When the user exits your application, Windows Phone automatically calls Save to save the isolated storage. Because you want to save the data only when the user explicitly clicks the Save button, not when the user exits the application, you must break the connection between the isolated storage and the data-bound object. To do that, you save a copy of the Accomplishment object instead of the actual object itself.(7) Adding the Application BarIn this application, the user chooses when they want to save their data by clicking a Save button. In this procedure, you add a Save button to the application bar. For the buttons icon, you will use one of the standard Windows Phone icons. For more information, see Application Bar for Windows Phone.实验详细设计如下:(1)Creating the Data ModelTo create the data model:1. In Solution Explorer, right-click the folder Model, point to Add and then click Class.The Add New Item dialog appears, with the class template selected.2. In the Name box, type Accomplishment.cs or Accomplishment.vb, and then click Add.A new class is added to the project in the Model folder, and opens in the code editor.3. Replace the code with the following code:using System;using System.ComponentModel;namespace MVVMTestApp.Model public class Accomplishment : INotifyPropertyChanged / The name of the accomplishment. public string Name get; set; / The type of the accomplishment, Item or Level. public string Type get; set; / The number of each item that has been collected. private int _count; public int Count get return _count; set _count = value; RaisePropertyChanged(Count); / Whether a level has been completed or not private bool _completed; public bool Completed get return _completed; set _completed = value; RaisePropertyChanged(Completed); public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(string propertyName) if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName); / Create a copy of an accomplishment to save. / If your object is databound, this copy is not databound. public Accomplishment GetCopy() Accomplishment copy = (Accomplishment)this.MemberwiseClone(); return copy; 4. On the File menu, click Save All. (Ctrl+Shift+S)5. On the Build menu, click Build Solution. (Ctrl+Shift+B) (2)Creating the ViewModel1. In Solution Explorer, right-click the folder ViewModel, point to Add and then click Class.The Add New Item dialog appears, with the class template selected.2. In the Name box, type ViewModel.cs or ViewModel.vb, and then click Add.A new class is added to the project in the ViewModel folder, and opens in the code editor.3. Replace the code with the following code:using System;using System.Windows;using System.Collections.ObjectModel;using System.IO.IsolatedStorage;using MVVMTestApp.Model;namespace MVVMTestApp.ViewModelNamespace public class ViewModel public ObservableCollection Accomplishments get; set; public void GetAccomplishments() if (IsolatedStorageSettings.ApplicationSettings.Count 0) GetSavedAccomplishments(); else GetDefaultAccomplishments(); public void GetDefaultAccomplishments() ObservableCollection a = new ObservableCollection(); / Items to collect a.Add(new Accomplishment() Name = Potions, Type = Item ); a.Add(new Accomplishment() Name = Coins, Type = Item ); a.Add(new Accomplishment() Name = Hearts, Type = Item ); a.Add(new Accomplishment() Name = Swords, Type = Item ); a.Add(new Accomplishment() Name = Shields, Type = Item ); / Levels to complete a.Add(new Accomplishment() Name = Level 1, Type = Level ); a.Add(new Accomplishment() Name = Level 2, Type = Level ); a.Add(new Accomplishment() Name = Level 3, Type = Level ); Accomplishments = a; /MessageBox.Show(Got accomplishments from default); public void GetSavedAccomplishments() ObservableCollection a = new ObservableCollection(); foreach (Object o in IsolatedStorageSettings.ApplicationSettings.Values) a.Add(Accomplishment)o); Accomplishments = a; /MessageBox.Show(Got accomplishments from storage); 4. On the File menu, click Save All. (Ctrl+Shift+S)5. On the Build menu, click Build Solution. (Ctrl+Shift+B)(3)Creating the First View1. In Solution Explorer, right-click the folder View, point to Add and then click New Item.2. In the list of file types, click Windows Phone User Control.3. In the Name box, type ItemView.xaml, and then click Add.A new XAML file is added to the project in the View folder, and opens in the designer. By default, there is an empty GRID element in the XAML file.4. In the GRID element, add the following code:XAMLCopy 5. On the File menu, click Save All. (Ctrl+Shift+S)6. On the Build menu, click Build Solution. (Ctrl+Shift+B)(4)Creating the Main Application Page1. In Solution Explorer, right-click MainPage.xaml and then click View Code.The code-behind file opens in the code editor.2. Replace the code with the following code:using System;using System.Linq;using System.Windows;using Microsoft.Phone.Controls;using MVVMTestApp.ViewModelNamespace;namespace MVVMTestApp public partial class MainPage : PhoneApplicationPage private ViewModel vm; / Constructor public MainPage() InitializeComponent(); vm = new ViewModel(); protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) base.OnNavigatedTo(e); / Later, you will replace this next line with something better. vm.GetAccomplishments(); / There are two different views, but only one view model. / So, use LINQ queries to populate the views. / Set the data context for the Item view. ItemViewOnPage.DataContext = from Accomplishment in vm.Accomplishments where Accomplishment.Type = Item select Accomplishment; / Set the data context for the Level view. LevelViewOnPage.DataContext = from Accomplishment in vm.Accomplishments where Accomplishment.Type = Level select Accomplishment; / If there is only one view, you could use the following code / to populate the view. /AccomplishmentViewOnPage.DataContext = vm.Accomplishments; 3. On the File menu, click Save All. (Ctrl+Shift+S)4. On the Build menu, click Build Solution. (Ctrl+Shift+B)(5)Maintaining Page State1. In Solution Explorer, right-click the project MVVMTestApp, point to Add and then click Class.2. In the Name box, type StateUtilities.cs or StateUtilities.vb, and then click Add.A new class is added to the project, and opens in the code editor.3. Replace the code with the following code:using System;namespace MVVMTestApp public static class StateUtilities private static Boolean isLaunching; public static Boolean IsLaunching get return isLaunching; set isLaunching = value; 4. In Solution Explorer, right-click App.xaml and then click View Code.5. Locate the Application_Launching method and replace it with the following code:private void Application_Launching(object sender, LaunchingEventArgs e) StateUtilities.IsLaunching = true;6. Locate the Application_Activated method and replace it with the following code:private void Application_Activated(object sender, ActivatedEventArgs e) StateUtilities.IsLaunching = false;7. In Solution Explorer, right-click MainPage.xaml and then click View Code.The code-behind file opens in the code editor.8. In the MainPage class, in the OnNavigatedTo method, locate the call to GetAccomplishments. It looks like the following:/ Later, you will replace this next line with something better.vm.GetAccomplishments();Replace it with the following code:if (!StateUtilities.IsLaunching & this.State.ContainsKey(Accomplishments) / Old instance of the application / The user started the application from the Back button. vm = (ViewModel)this.StateAccomplishments; /MessageBox.Show(Got data from state);else / New instance of the application / The user started the application from the application list, / or there is no saved state available. vm.GetAccomplishments(); /MessageBox.Show(Did not get data from state);9. In the MainPage class, after the OnNavigatedTo method, add the following code:protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) base.OnNavigatedFrom(e); if (this.State.ContainsKey(Accomplishments) this.StateAccomplishments = vm; else this.State.Add(Accomplishments, vm); StateUtilities.IsLaunching = false;10. On the File menu, click Save All.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025浙江省商务究院实习人员招聘笔试备考题库及答案解析
- 2025浙江金华市武义县司法局招聘4人笔试参考题库附答案解析
- 2025云南省丽江市玉龙纳西族自治县幼儿园招聘公益性岗位教师(3人)笔试备考试题及答案解析
- 养殖业标准化建设方案
- 2025新疆兵团粮安储备粮管理有限责任公司招聘19人考试含答案
- 2025西安国际港务区陆港第七小学招聘笔试备考试题及答案解析
- 2025年铁岭银行见习生招聘50人考试备考试题及答案解析
- 2025年体育专业中级运动防护师考试真题附答案
- 2025年事业单位工勤技能-广东-广东计算机文字录入处理员五级(初级工)历年参考题库含答案解析5套
- 2025年学校公共卫生管理实务案例分析答案及解析
- 2024秋七年级数学上册 第1章 有理数1.2 数轴、相反数和绝对值 2相反数教学实录(新版)沪科版
- 安全防坠网施工方案
- 六年级语文毕业考试真题集锦(共9套含答案)
- 工程造价职业技能比武竞赛参考试题(附答案)
- 天津第一中学2025-2025学年高三下学期3月月考英语试卷(含答案)
- 跨部门药事管理的职责与协作机制
- 农场生态农业循环产业园项目方案书
- 新人教版7年级上册英语全册课件(2024年新版教材)
- 合同权利转让合同范例
- 有组织科研对高校拔尖创新人才培养的影响机制研究
- 突发传染病疫情应急
评论
0/150
提交评论