Silverlight学习(二)
好久没来写博客了,这期间经历了春节,也因为忙于一个项目,所以博客被疏忽了。最近一段时间一直在用silverlight做项目,从来一开始的不熟悉渐渐的开始上手。今天记录一下自己学习prism的一些samplecode。
silvierlight目前的主流架构是Silverlight+MVVM+WCF RIA,说来惭愧本人做项目的时候对设计模式不是很了解。MVVM设计模式是指模型(Model)-视图(View)-视图模型(ViewModel),MVVM设计模式能够将程序的UI设计和逻辑设计分开,这样能够节省开发人员的大量时间,也可以使代码更容易维护和升级等。View是指UI,是用来展示的,Model可以定义一些数据访问的实体类,ViewModel是连接model层和view层的桥梁,它是中间层,主要用来一些业务逻辑的设计,这里包括与数据库的交互。
Prism是微软提供的一个用于Silverlight和WPF开发的框架。
下面重点讲讲Prim+MVVM的实现。
1.需要新建一个Silverlight应用程序,分为Silverlight服务端和客户端两部分,需要在Silverlight客户端添加View、Model、ViewModel几个文件夹,分别对应MVVM设计模式。
2.在Model中添加类Questionnaire
/// <summary>
/// 定义Model,如果需要监听属性的变化,需要继承INotifyPropertyChanged类
/// </summary>
public class Questionnaire:INotifyPropertyChanged
{
private string favoriteColor;
public Questionnaire()
{
}
public event PropertyChangedEventHandler PropertyChanged; public string Name { get; set; } public int Age { get; set; } public string Quest { get; set; } public string FavoriteColor
{
get
{
return this.favoriteColor;
} set
{
//监听颜色属性的变化
if (value != this.favoriteColor)
{
this.favoriteColor = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("FavoriteColor"));
}
}
}
}
private string getText;
public string GetText
{
get { return getText; }
set
{
//监听字符的变化
if (value != this.getText)
{
this.getText = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("GetText"));
}
}
}
}
}
Model
3.添加在modelview文件夹中添加questionnaireviewmodel类
/// <summary>
/// 定义viewModel
/// </summary>
public class QuestionnaireViewModel
{
private readonly Questionnaire questionnaire; public QuestionnaireViewModel()
{
this.questionnaire = new Questionnaire();
this.AllColors = new[] { "Red", "Blue", "Green" }; this.SubmitCommand = new DelegateCommand<object>(this.OnSubmit);//实例化一个command,DelegateCommand引用类库 Microsoft.Practices.Prism.Commands }
/// <summary>
/// 定义Model的属性
/// </summary>
public Questionnaire Questionnaire
{
get { return this.questionnaire; }
} public IEnumerable<string> AllColors { get; private set; }
/// <summary>
/// 定义一个command,可以绑定到控件上
/// </summary>
public ICommand SubmitCommand { get; private set; } private void OnSubmit(object obj)
{
questionnaire.GetText = this.BuildResultString().ToString(); } private string BuildResultString()
{
StringBuilder builder = new StringBuilder();
builder.Append("Name: ");
builder.Append(this.questionnaire.Name);
builder.Append("\nAge: ");
builder.Append(this.questionnaire.Age);
builder.Append("\nQuestion 1: ");
builder.Append(this.questionnaire.Quest);
builder.Append("\nQuestion 2: ");
builder.Append(this.questionnaire.FavoriteColor);
return builder.ToString();
}
}
4.在view文件夹中添加QuestionView,用来显示
d:DesignHeight="" d:DesignWidth="">
<!--绑定ViewModel,获取上下文消息,这里面一般包括需要绑定的字段、类、方法等-->
<UserControl.DataContext>
<vm:QuestionnaireViewModel></vm:QuestionnaireViewModel>
</UserControl.DataContext>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height=""/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <ScrollViewer Grid.Row="" Grid.ColumnSpan="" VerticalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions> <Border Grid.Row="" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions> <TextBlock Grid.Column="" Grid.Row="" VerticalAlignment="Bottom"><Run Text="Name"/></TextBlock>
<TextBox x:Name="NameTextBox" Grid.Column="" Grid.Row="" Text="{Binding Questionnaire.Name, Mode=TwoWay}" Width="" Margin="" /> <TextBlock Grid.Column="" Grid.Row="" VerticalAlignment="Bottom"><Run Text="Age"/></TextBlock>
<TextBox x:Name="AgeTextBox" Grid.Column="" Grid.Row="" HorizontalAlignment="Left" Text="{Binding Questionnaire.Age, Mode=TwoWay}" MaxLength="" Width="" Margin="" /> </Grid>
</Border> <Grid Grid.Row="" Margin="" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions> <TextBlock Grid.Row="" Text="What is your quest?" Style="{StaticResource QuestionLabelStyle}" />
<TextBox x:Name="Question1Response" Grid.Row="" Text="{Binding TestClass.Quest, Mode=TwoWay}" />
</Grid> <Grid Grid.Row="" Margin="">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions> <TextBlock Grid.Row="" Style="{StaticResource QuestionLabelStyle}">
<Run Text="What is your favorite "/><Run x:Name="ColorRun" Text="color" Foreground="{Binding Questionnaire.FavoriteColor, TargetNullValue=Black}"/><Run Text="?"/>
</TextBlock> <Border Grid.Row="" Style="{StaticResource BorderBrush}">
<ListBox x:Name="Colors" IsTabStop="False" ItemsSource="{Binding AllColors}" Margin=""
SelectedItem="{Binding Questionnaire.FavoriteColor, Mode=TwoWay}">
</ListBox>
</Border>
</Grid>
</Grid>
</ScrollViewer>
<TextBlock Grid.Row="" Grid.Column="" Text="{Binding Path=Questionnaire.GetText,Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="" Height=""></TextBlock>
<Button x:Name="SubmitButton" Command="{Binding SubmitCommand}" Grid.Row="" Grid.Column="" Content="Submit" Height="" HorizontalAlignment="Right" Width=""/>
</Grid>
View
设计前台,并未控件绑定数据。其中Button控件绑定了无参数的ICommand命令,后台为DelegateCommand。通过Button控件 我们可以获取到数据源的变化,并将它显示到页面上。。
5.在MainPage主页面添加已经设计好的页面。
<UserControl xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SilverlightApplication4.MainPage"
xmlns:view="clr-namespace:SilverlightApplication4.View"
xmlns:viewModel="clr-namespace:SilverlightApplication4.ViewModel"
Width="" Height="">
<Grid x:Name="LayoutRoot" Background="White" >
<view:QuestionnaireView></view:QuestionnaireView>
<!--<view:PersonViewList DataContext="myele"></view:PersonViewList>-->
</Grid>
</UserControl>
Main
这样就玩一个了基本的Silverlight应用程序,本程序未设计到与数据库的交互,下一篇将会有所涉及。
Silverlight学习(二)的更多相关文章
- ArcGIS api fo silverlight学习二(silverlight加载GraphicsLayer)
上一节学习了silverlight加载GeoServer发布的WMS地图,这一节学习一下加载GraphicsLayer 一.加载.png或jpg文件图标 1.在MainPage.xaml中添加资源配置 ...
- ArcGIS API for Silverlight学习笔记
ArcGIS API for Silverlight学习笔记(一):为什么要用Silverlight API(转) 你用上3G手机了吗?你可能会说,我就是喜欢用nokia1100,ABCDEFG跟我都 ...
- emberjs学习二(ember-data和localstorage_adapter)
emberjs学习二(ember-data和localstorage_adapter) 准备工作 首先我们加入ember-data和ember-localstorage-adapter两个依赖项,使用 ...
- ReactJS入门学习二
ReactJS入门学习二 阅读目录 React的背景和基本原理 理解React.render() 什么是JSX? 为什么要使用JSX? JSX的语法 如何在JSX中如何使用事件 如何在JSX中如何使用 ...
- TweenMax动画库学习(二)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- Hbase深入学习(二) 安装hbase
Hbase深入学习(二) 安装hbase This guidedescribes setup of a standalone hbase instance that uses the local fi ...
- Struts2框架学习(二) Action
Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...
- Python学习二:词典基础详解
作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...
- Quartz学习--二 Hello Quartz! 和源码分析
Quartz学习--二 Hello Quartz! 和源码分析 三. Hello Quartz! 我会跟着 第一章 6.2 的图来 进行同步代码编写 简单入门示例: 创建一个新的java普通工程 ...
- SpringCloud学习(二):微服务入门实战项目搭建
一.开始使用Spring Cloud实战微服务 1.SpringCloud是什么? 云计算的解决方案?不是 SpringCloud是一个在SpringBoot的基础上构建的一个快速构建分布式系统的工具 ...
随机推荐
- [英国][记录][战争中的世界:二战全史(26集)][BD-MKV/58G][中英双字][经典收藏]
[英国][记录][战争中的世界:二战全史(26集)][BD-MKV/58G][中英双字][经典收藏] 原片名:The World at War 中文名:战争中的世界 导 演:Ted Childs, ...
- LR实战之Discuz开源论坛——网页细分图结果分析(Web Page Diagnostics)
续LR实战之Discuz开源论坛项目,之前一直是创建虚拟用户脚本(Virtual User Generator)和场景(Controller),现在,终于到了LoadRunner性能测试结果分析(An ...
- sqlplus常用操作命令2
常用编辑命令:A[ppend] text 将text附加到当前行之后C[hange] /old /new 将当前行中的old替换为newCLear] buff[er] 清除缓冲区中的所有行DEL 删除 ...
- 【二分查找+优化O(n)】【续UVA1121】Subsequence
之前的二分答案做法 http://blog.csdn.net/zy691357966/article/details/40212215 二分查找做法: 我们首先试试只枚举终点.对于终点j,我们的目标是 ...
- hdu2206IP的计算
Problem Description 在网络课程上,我学到了很多有关IP的知识.IP全称叫网际协议,有时我们又用IP来指代我们的IP网络地址,现在IPV4下用一个32位无符号整数来表示,一般用点分方 ...
- CheckStyle插件
如今代码静态检查越来越重要,已经成为构建高质量软件的不可或缺的一个验证步骤.如果你使用的是java语言,那么CheckStyle则是一个利器. CheckStyle能够帮助程序员检查代码是否符合制定的 ...
- 多线程程序中fork导致的一些问题
最近项目中,在使用多线程和多进程时,遇到了些问题. 问题描述:在多线程程序中fork出一个新进程,发现新的进程无法正常工作. 解决办法:将开线程的代码放在fork以后.也就是放在新的子进程中进行创建. ...
- win32 清空ListBox所有内容
Q:clear listbox hi i am working in VC++ 6 using Win32 App. .............tell me how to clear the lis ...
- python cmd模块练习
# encoding=utf-8 import cmd import sys # cmd模块练习 class Client(cmd.Cmd): ''' 1)cmdloop():类似与Tkinter的m ...
- flask-script 安装问题
> 昨天开始看 flask,第二章最后提到使用 Flask-Script 支持命令行选项,但是用书上的方法安装却出现了问题. 错误信息: 注意最后两行: Could not find a ver ...