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的基础上构建的一个快速构建分布式系统的工具 ...
随机推荐
- 谋哥:研究App排行榜浮出的神器
昨天发的<App排行榜的秘密>到头条网,阅读量到2万,踩的比顶的多几倍.原因是由于我使用360市场的数据来分析,而且这帮喷子根本不看你分析数据背后的意义,反正看到自己不喜欢的比方" ...
- 如何统一删除word中的超链接
[摘要] 我们从别处拷贝文字,或从网上复制的文字,里面有很多超级链接,如何可以批量删除这些链接呢?这里介绍两种批量删除链接的方法. [正文] 方法一:使用快捷键删除超链接 有个神奇的快捷键,可以帮我们 ...
- visual studio 2010 C语言声明异常
如下这段程序,是C_Primer_plus_第五版内的一个复习题答案(感觉声明i的值有问题),在GCC上面可以运行,但是移植到VS2010就一堆错误, #include<stdio.h> ...
- linux下SSH远程连接服务慢解决方案
1.适用命令及方案如下:[远程连接及执行命令]ssh -p22root@10.0.0.19ssh -p22 root@10.0.0.19 /sbin/ifconfig[远程拷贝:推送及拉取]scp - ...
- 关于JavaScript 原型的理解
原型的含义是指:如果构造器有个原型对象A,则由该构造器创建的实例(Object Instance)都必然复制于A.““在JavaScript中,对象实例(Object Instance)并没有原型,而 ...
- pl sql练习(4)
1.ROW_NUMBER 返回连续的排位,不论值是否相等 select deptno,ename,sal, row_number () over (partition by deptno order ...
- XMAL 中x名称控件的Auttribute
1 X:Class 作用告诉XAML编译器将XAML标签的编译结果与后台代码中指定的类合并,只能用于根节点,并且与之同名的类需要有Partial 例如窗口 2 X:ClassModifier 作用告诉 ...
- uva 123 Searching Quickly
Searching Quickly Background Searching and sorting are part of the theory and practice of computer ...
- Node.js HTTP 使用详解
对于初学者有没有发觉在查看Node.js官方API的时候非常简单,只有几个洋文描述两下子,没了,我第一次一口气看完所以API后,对于第一个示例都有些懵,特别是参数里的request和response, ...
- 【python】中文的输出,打印,文件编码问题解决方法
直接在python中输入中文的字符串会报编译错误SyntaxError: Non-ASCII character,因为python文件默认编码方式是ASCII.如果想要打印中文字符,有两种方式: 1. ...