稳扎稳打Silverlight(34) - 3.0控件之Frame, Page, Label, DescriptionViewer, ValidationSummary

[索引页]
[源码下载]


稳扎稳打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://webabcd.blog.51cto.com/1787395/342289

示例
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 
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif using System.Windows.Navigation; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Control 
InBlock.gif
InBlock.gif         public partial  class Frame : Page 
InBlock.gif        { 
InBlock.gif                 public Frame() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void navigateToPageDemo_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         /* 
InBlock.gif                         * Navigate() - 导航到指定的 Uri 地址 
InBlock.gif                         * CanGoBack - 是否可后退 
InBlock.gif                         * CanGoForward - 是否可前进 
InBlock.gif                         * GoBack() - 后退 
InBlock.gif                         * GoForward() - 前进 
InBlock.gif                         */
 
InBlock.gif                         
InBlock.gif                        frame.Navigate( new Uri( "/Control/PageDemo", UriKind.Relative)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void navigateToPageDemo2_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        frame.Navigate( new Uri( "/Control/PageDemo2", UriKind.Relative)); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
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
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif using System.Windows.Navigation; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Control 
InBlock.gif
InBlock.gif         public partial  class PageDemo : Page 
InBlock.gif        { 
InBlock.gif                 public PageDemo() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 // 当用户导航至此控件时,会调用如下方法 
InBlock.gif                 protected  override  void OnNavigatedTo(NavigationEventArgs e) 
InBlock.gif                { 
InBlock.gif                         /* 
InBlock.gif                         * NavigationService - 在 Page 控件中做导航的类 
InBlock.gif                         * NavigationContext - 导航的上下文,其 QueryString 属性可用于获取导航参数 
InBlock.gif                         * NavigationEventArgs.Uri - 当前导航地址 
InBlock.gif                         */
 
InBlock.gif 
InBlock.gif                        lblMsg.Text +=  "当前的导航地址:" + e.Uri.ToString() +  "\n"
InBlock.gif 
InBlock.gif                         if ( this.NavigationContext.QueryString.ContainsKey( "param1")) 
InBlock.gif                                lblMsg.Text +=  "参数1:" + NavigationContext.QueryString[ "param1"] +  "\n"
InBlock.gif                         if ( this.NavigationContext.QueryString.ContainsKey( "param2")) 
InBlock.gif                                lblMsg.Text +=  "参数2:" + NavigationContext.QueryString[ "param2"]; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void Button_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         if (((System.Windows.Controls.Frame) this.Parent).UriMapper ==  null
InBlock.gif                                NavigationService.Navigate( new Uri( "/Control/PageDemo2.xaml", UriKind.Relative)); 
InBlock.gif                         else 
InBlock.gif                                NavigationService.Navigate( new Uri( "/Control/PageDemo2", UriKind.Relative)); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
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
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif using System.Windows.Navigation; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Control 
InBlock.gif
InBlock.gif         public partial  class PageDemo2 : Page 
InBlock.gif        { 
InBlock.gif                 public PageDemo2() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void Button_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         if (((System.Windows.Controls.Frame) this.Parent).UriMapper ==  null
InBlock.gif                                NavigationService.Navigate( new Uri( "/Control/PageDemo.xaml?param1=param1¶m2=param2", UriKind.Relative)); 
InBlock.gif                         else 
InBlock.gif                                NavigationService.Navigate( new Uri( "/Control/PageDemo?param1=param1¶m2=param2", UriKind.Relative)); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
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, Label, DescriptionViewer 的结合使用,实现数据验证的 UI 部分。验证的逻辑部分由 System.ComponentModel.DataAnnotations 实现
EmployeeModel.cs
InBlock.gif /* 
InBlock.gif * Silverlight 支持 System.ComponentModel.DataAnnotations 方式的数据验证。同样支持该数据验证的还有 Dynamic Data, asp.net mvc 2 
InBlock.gif */
 
InBlock.gif 
InBlock.gif using System; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Ink; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.ComponentModel.DataAnnotations; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Model 
InBlock.gif
InBlock.gif         public  class EmployeeModel 
InBlock.gif        { 
InBlock.gif                 private  string _name; 
InBlock.gif                [Display(Name =  "名字", Description =  "必填字段")] 
InBlock.gif                [Required(ErrorMessage= "名字必填")] 
InBlock.gif                 public  string Name 
InBlock.gif                { 
InBlock.gif                        get {  return _name; } 
InBlock.gif                        set 
InBlock.gif                        { 
InBlock.gif                                 /* 
InBlock.gif                                 * Validator.ValidateProperty() - 用于决定指定的属性是否通过了验证(根据属性的 DataAnnotations 的 Attribute 做判断)。以及当其没有通过验证时,抛出异常 
InBlock.gif                                 */
 
InBlock.gif                                Validator.ValidateProperty(value,  new ValidationContext( thisnullnull) { MemberName =  "Name" }); 
InBlock.gif                                _name = value; 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  double _salary; 
InBlock.gif                [Display(Name= "薪水", Description= "薪水介于 0 - 10000 之间")] 
InBlock.gif                [Range(0,10000)] 
InBlock.gif                 public  double Salary 
InBlock.gif                { 
InBlock.gif                        get {  return _salary; } 
InBlock.gif                        set 
InBlock.gif                        { 
InBlock.gif                                Validator.ValidateProperty(value,  new ValidationContext( thisnullnull) { MemberName =  "Salary" }); 
InBlock.gif                                _salary = value; 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public DateTime DateOfBirty { get; set; } 
InBlock.gif        } 
InBlock.gif}
 
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, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}" /> 
                                        <dataInput:DescriptionViewer Target="{Binding ElementName=employee}" PropertyPath="Salary" /> 
                                         
                                </StackPanel> 
                        </StackPanel> 

                        <!-- 
                                ValidationSummary - 汇总显示验证错误的信息 
                                SummaryListBoxStyle - 显示汇总错误信息的 ListBox 控件的样式 
                        --> 
                        <dataInput:ValidationSummary /> 

                </StackPanel> 
        </Grid> 
</navigation:Page>
 
ValidationSummary.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif using System.Windows.Navigation; 
InBlock.gif 
InBlock.gif using Silverlight30.Model; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Control 
InBlock.gif
InBlock.gif         public partial  class ValidationSummary : Page 
InBlock.gif        { 
InBlock.gif                 public ValidationSummary() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.Loaded +=  new RoutedEventHandler(ValidationSummary_Loaded); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void ValidationSummary_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         this.DataContext =  new EmployeeModel() { Name =  "webabcd", Salary = 0 }; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
 

     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/342748 ,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值