[源码下载]
稳扎稳打Silverlight(34) - 3.0控件之Frame,Page,Label,DescriptionVIEwer,ValIDationSummary
作者: webabcd
介绍
Silverlight 3.0 控件一览:
Frame - 与 Page 控件结合使用,从而实现导航功能(可以由此实现 Deep linking) Page - 与 Frame 控件结合使用 Label - 比 TextBlock 功能多一些,可以用来对错误的验证信息做提示 DescriptionVIEwer - 鼠标经过时的提示信息 ValIDationSummary - 汇总显示验证错误的信息
在线DEMO
http://www.cnblogs.com/webabcd/archive/2009/08/04/1538238.html
示例
1、Frame 控件的使用演示。其可以导航 Page,可以做url映射
Frame.xaml
< navigation:Page x:Class ="Silverlight30.Control.Frame"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
xmlns:uriMapper ="clr-namespace:System.windows.Navigation;assembly=System.windows.Controls.Navigation"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d"
d:DesignWIDth ="640" d:DesignHeight ="480"
Title ="Frame Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel HorizontalAlignment ="left" >
< border borderBrush ="Gray" borderThickness ="3" padding ="10" >
<!--
Frame - 与 Page 控件结合使用,从而实现导航功能(可以由此实现 Deep linking)
Source - 需要在 Frame 中显示的 Page 的地址
JournalOwnership - 导航日志的记录方式 [System.windows.Navigation.JournalOwnership 枚举]
automatic - 如果 Frame 是最顶级的 Frame,则在浏览器端记录导航日志,否则由此 Frame 自行记录
OwnsJournal - 自行记录
UsesParentJournal - 当 Frame 是最顶级的 Frame 时,由浏览器记录。如果是非顶级 Frame 的话,则会抛出异常
UriMapper - Uri 映射器。可以在其内编辑映射规则
UriMapPing - 具体的映射规则(在 System.windows.Navigation 命名空间下)
如本例就是把类似 Silverlight30TestPage.aspx#/Control/PageDemo 的地址映射到类似 Silverlight30TestPage.aspx#/Control/PageDemo.xaml 的地址
-->
< navigation:Frame x:name ="frame" Source ="/Control/PageDemo" JournalOwnership ="OwnsJournal" >
< navigation:Frame.Content >
< TextBlock Text ="我是 Frame 的 Content" />
</ navigation:Frame.Content >
< navigation:Frame.UriMapper >
< uriMapper:UriMapper >
< uriMapper:UriMapPing Uri ="/{address}" MappedUri ="/{address}.xaml" />
</ uriMapper:UriMapper >
</ navigation:Frame.UriMapper >
</ navigation:Frame >
</ border >
< button x:name ="navigatetoPageDemo" Content ="链接到 PageDemo" Click ="navigatetoPageDemo_Click" WIDth ="200" />
< button x:name ="navigatetoPageDemo2" Content ="链接到 PageDemo2" Click ="navigatetoPageDemo2_Click" WIDth ="200" />
</ StackPanel >
</ GrID >
</ navigation:Page >
Frame.xaml.cs
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using System.windows.Navigation;
namespace Silverlight30.Control
{
public partial class Frame : Page
{
public Frame()
{
InitializeComponent();
}
private voID navigatetoPageDemo_Click(object sender, RoutedEventArgs e)
@H_419_735@
{/**//*
* Navigate() - 导航到指定的 Uri 地址
* CanGoBack - 是否可后退
* CanGoForward - 是否可前进
* GoBack() - 后退
* GoForward() - 前进
*/
frame.Navigate(new Uri("/Control/PageDemo", UriKind.relative));
}
private voID navigatetoPageDemo2_Click(object sender, RoutedEventArgs e)
{
frame.Navigate(new Uri("/Control/PageDemo2", UriKind.relative));
}
}
}
2、Page 控件的使用演示。在 Page 间做导航,以及之间的参数传递
PageDemo.xaml
< navigation:Page x:Class ="Silverlight30.Control.PageDemo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
d:DesignWIDth ="640" d:DesignHeight ="480"
Title ="PageDemo Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel >
< TextBlock Text ="我是 PageDemo" />
< button Content ="链接到 PageDemo2" Click ="button_Click" />
< TextBlock x:name ="lblMsg" />
</ StackPanel >
</ GrID >
</ navigation:Page >
PageDemo.xaml.cs
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using System.windows.Navigation;
namespace Silverlight30.Control
{
public partial class PageDemo : Page
{
public PageDemo()
{
InitializeComponent();
}
// 当用户导航至此控件时,会调用如下方法
protected overrIDe voID OnNavigatedTo(NavigationEventArgs e)
{
/**//*
* NavigationService - 在 Page 控件中做导航的类
* NavigationContext - 导航的上下文,其 queryString 属性可用于获取导航参数
* NavigationEventArgs.Uri - 当前导航地址
*/
lblMsg.Text += "当前的导航地址:" + e.Uri.ToString() + "/n";
if (this.NavigationContext.queryString.ContainsKey("param1"))
lblMsg.Text += "参数1:" + NavigationContext.queryString["param1"] + "/n";
if (this.NavigationContext.queryString.ContainsKey("param2"))
lblMsg.Text += "参数2:" + NavigationContext.queryString["param2"];
}
private voID button_Click(object sender, RoutedEventArgs e)
{
if (((System.windows.Controls.Frame)this.Parent).UriMapper == null)
NavigationService.Navigate(new Uri("/Control/PageDemo2.xaml", UriKind.relative));
else
NavigationService.Navigate(new Uri("/Control/PageDemo2", UriKind.relative));
}
}
}
PageDemo2.xaml
< navigation:Page x:Class ="Silverlight30.Control.PageDemo2"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
d:DesignWIDth ="640" d:DesignHeight ="480"
Title ="PageDemo2 Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel >
< TextBlock Text ="我是 PageDemo2" />
< button Content ="链接到 PageDemo" Click ="button_Click" />
</ StackPanel >
</ GrID >
</ navigation:Page >
PageDemo2.xaml.cs
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using System.windows.Navigation;
namespace Silverlight30.Control
{
public partial class PageDemo2 : Page
{
public PageDemo2()
{
InitializeComponent();
}
private voID button_Click(object sender, RoutedEventArgs e)
{
if (((System.windows.Controls.Frame)this.Parent).UriMapper == null)
NavigationService.Navigate(new Uri("/Control/PageDemo.xaml?param1=param1¶m2=param2", UriKind.relative));
else
NavigationService.Navigate(new Uri("/Control/PageDemo?param1=param1¶m2=param2", UriKind.relative));
}
}
}
3、演示 Label 控件
Label.xaml
< navigation:Page xmlns:datainput ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Data.input" x:Class ="Silverlight30.Control.Label"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
d:DesignWIDth ="640" d:DesignHeight ="480"
Title ="Label Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel >
<!-- Label 控件的演示 -->
< datainput:Label Content ="我是 Label" Foreground ="White" >
< datainput:Label.Background >
< linearGradIEntBrush StartPoint ="0,0" EndPoint ="1,1" >
< GradIEntStop color ="Red" Offset ="0" />
< GradIEntStop color ="Green" Offset ="0.5" />
< GradIEntStop color ="Blue" Offset ="1" />
</ linearGradIEntBrush >
</ datainput:Label.Background >
</ datainput:Label >
</ StackPanel >
</ GrID >
</ navigation:Page >
4、演示 DescriptionVIEwer 控件
DescriptionVIEwer.xaml
< navigation:Page xmlns:datainput ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Data.input" x:Class ="Silverlight30.Control.DescriptionVIEwer"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
d:DesignWIDth ="640" d:DesignHeight ="480"
Title ="DescriptionVIEwer Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel margin ="10" >
<!--
Description - 鼠标经过时的提示信息
GlyphTemplate - 显示提示信息的图标部分的外观
-->
< datainput:DescriptionVIEwer Description ="设置 DescriptionVIEwer 的 Description 属性" />
</ StackPanel >
</ GrID >
</ navigation:Page >
5、ValIDationSummary,DescriptionVIEwer 的结合使用,实现数据验证的 UI 部分。验证的逻辑部分由 System.ComponentModel.DataAnnotations 实现
EmployeeModel.cs
/**/ /*
* Silverlight 支持 System.ComponentModel.DataAnnotations 方式的数据验证。同样支持该数据验证的还有 Dynamic Data, asp.net mvc 2
*/
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.DataAnnotations;
namespace Silverlight30.Model
{
public class EmployeeModel
{
private string _name;
[display(name = "名字", Description = "必填字段")]
[required(ErrorMessage="名字必填")]
public string name
{
@H_891_3010@
@H_564_3013@
get { return _name; }set
{
/**//*
* ValIDator.ValIDateproperty() - 用于决定指定的属性是否通过了验证(根据属性的 DataAnnotations 的 Attribute 做判断)。以及当其没有通过验证时,抛出异常
*/
ValIDator.ValIDateProperty(value, new ValIDationContext(this, null, null) { Membername = "name" });
_name = value;
}
}
private double _salary;
[display(name="薪水", Description="薪水介于 0 - 10000 之间")]
[Range(0,10000)]
public double Salary
{
get { return _salary; }
set
@H_351_3270@
{ValIDator.ValIDateProperty(value, null) { Membername = "Salary" });
_salary = value;
}
}
public DateTime DateOfBirty { get; set; }
}
}
ValIDationSummary.xaml
< navigation:Page xmlns:datainput ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Data.input" x:Class ="Silverlight30.Control.ValIDationSummary"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/Expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable ="d"
xmlns:navigation ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
d:DesignWIDth ="640" d:DesignHeight ="480"
Title ="ValIDationSummary Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel >
< StackPanel x:name ="employee" >
< StackPanel OrIEntation ="Horizontal" >
<!--
Label - 可以用来对错误的验证信息做提示。默认为将文本变为红色
DescriptionVIEwer - 其 Description 属性可以自动绑定到指定属性的 display 特性上
Target - 关联的对象,以对相应的元数据(Metadata)做提示
PropertyPath - 所关联的对象的指定的字段
-->
< datainput:Label Target =" {Binding Elementname=name} " />
< TextBox x:name ="name" Text =" {Binding name, Mode=TwoWay, NotifyOnValIDationError=True, ValIDatesOnExceptions=True} " />
< datainput:DescriptionVIEwer Target =" {Binding Elementname=employee} " PropertyPath ="name" />
</ StackPanel >
< StackPanel OrIEntation ="Horizontal" >
< datainput:Label Target =" {Binding Elementname=salary} " />
< TextBox x:name ="salary" Text =" {Binding Salary, ValIDatesOnExceptions=True} " />
< datainput:DescriptionVIEwer Target =" {Binding Elementname=employee} " PropertyPath ="Salary" />
</ StackPanel >
</ StackPanel >
<!--
ValIDationSummary - 汇总显示验证错误的信息
SummaryListBoxStyle - 显示汇总错误信息的 ListBox 控件的样式
-->
< datainput:ValIDationSummary />
</ StackPanel >
</ GrID >
</ navigation:Page >
ValIDationSummary.xaml.cs
using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using System.windows.Navigation;
using Silverlight30.Model;
namespace Silverlight30.Control
{
public partial class ValIDationSummary : Page
@H_31_4045@
@H_403_4048@
{public ValIDationSummary()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(ValIDationSummary_Loaded);
}
voID ValIDationSummary_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = new EmployeeModel() { name = "webabcd", Salary = 0 };
}
}
}
OK
[源码下载] 总结
以上是内存溢出为你收集整理的稳扎稳打Silverlight(34) - 3.0控件之Frame, Page, Label, DescriptionViewer, ValidationSummary全部内容,希望文章能够帮你解决稳扎稳打Silverlight(34) - 3.0控件之Frame, Page, Label, DescriptionViewer, ValidationSummary所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)