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的基础上构建的一个快速构建分布式系统的工具 ...
随机推荐
- Python 练习 —— 2048
1. 引言 2048 这段时间火的不行啊,大家都纷纷仿造,"百家争鸣",于是出现了各种技术版本号:除了手机版本号,还有C语言版.Qt版.Web版.java版.C#版等,刚好我接触P ...
- Android系统的开机画面显示过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7691321 好几个月都没有更新过博客了,从今天 ...
- smaba服务的搭建
一. samba配置1. 什么是sambaSamba服务类似于windows上的共享功能,可以实现在Linux上共享文件,windows上访问,当然在Linux上也可以访问到.是一种在局域网上共享文件 ...
- Windows下安装Memcache
安装步骤的时候只需要做两步: 第一步:安装memcache.exe 服务. 第二步:安装php_memcache.dll扩展,让php支持memcache. 1.安装 memcache.exe 服务 ...
- SQL Server 2008 修改表名
有一张表 修改起 if exists (select * from sys.objects where object_id = object_id(N'Table_1') and type in ...
- eclipse安装svn插件,在输入url后,一直卡在in progress界面不懂。
今天遇到上面的情况.网上找了半天都没有找到解决的办法.后来,仔细比对了一下我的eclipse版本和svn版本.发现svn版本真的太老了.用上新的svn后,立马就可以用了 svn - http://su ...
- 求一组数字序列的分布情况(java)
最近需要做一个正态分布的函数图像所以要处理一段double序列 写了这个算法 先上效果图: 核心思想: 1先根据步长计算每一个区间 2循环进行判断序列中每个数属于哪个区间 3用一个数组来保存每一个区 ...
- POJ 2594 - Treasure Exploration
一个星球上有很多点,点与点之间有很多单向路 问可重点的最小路径覆盖 利用floyd缩点后求二分图最大匹配 #include <iostream> #include <cstdio&g ...
- MFC 控件初始化的过程
之前为了学习MFC下浏览器的用法,参考博文:http://www.cnblogs.com/firefly_liu/archive/2009/05/18/1459514.html,虽然按照作者的方法实现 ...
- (转)(VS2013 )由于应用程序配置不正确,程序未能启动”--原因及解决方法
今天把别人的程序拿过来编译时通过,但是运行的时候,提示:由于应用程序配置不正确,程序未能启动 搜了一下,各种方法.最终通过下面的方法解决的. 项目--->配置属性---->链接器----& ...