前言:Silverlight 2.0 Beta1 已经发布,加入了许多激动人心的新特性:WPF UI 框架、丰富的控件、丰富的网络支持、丰富的基础类库支持等。这是本人的学习笔记,写的比较乱,因此定名为乱弹琴系列 Silverlight 2.0 文章。
本文内容:介绍一些Silverlight中的基本控件。
1.UserControl:
继承层次:
System.Object
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Media.Visual
System.Windows.UIElement
System.Windows.FrameworkElement
System.Windows.Controls.Control
System.Windows.Controls.ContentControl
System.Windows.Controls.UserControl
示例:
<UserControl x:Class="FirstSilverlight.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
</UserControl>
xmlns="http://schemas.microsoft.com/client/2007" 系统默认的命名空间
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:对应的自定义的命名空间,可以修改成任何值(要符合标识符的命名规则),如:y
x:Class="FirstSilverlight.Page" FirstSilverlight.Page 处理该页事件等等的类,需要全名,在此"Page"为类名,"FirstSilverlight"是类"Page"的命名空间,如下代码。
namespace FirstSilverlight
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
}
}
上面的 InitializeComponent(); 来自哪里?可打开your project\obj\Debug,发现里面有一些"XXXX.g.cs",这就是InitializeComponent(); 的代码。我测试页的代码如下:
#pragma checksum "e:\Projects\FirstSilverlight\FirstSilverlight\Page.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "8F7EA1DE0CB5C723096A889393DD4BEB"
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.1433
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Hosting;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace FirstSilverlight {
public partial class Page : System.Windows.Controls.UserControl {
internal System.Windows.Controls.Canvas cvs1;
internal System.Windows.Controls.TextBox txt;
internal WatermarkedTextBox txt2;
private bool _contentLoaded;
/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Windows.Application.LoadComponent(this, new System.Uri("/FirstSilverlight;component/Page.xaml", System.UriKind.Relative));
this.cvs1 = ((System.Windows.Controls.Canvas)(this.FindName("cvs1")));
this.txt = ((System.Windows.Controls.TextBox)(this.FindName("txt")));
this.txt2 = ((WatermarkedTextBox)(this.FindName("txt2")));
}
}
}
UserControl 默认作为 xaml的root element,内部只能含有一个子控件,因为它不是布局控件。
UserControl有许多事件、方法、属性等,可参考文档。
2.Button:
继承层次:
System.Object
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Media.Visual
System.Windows.UIElement
System.Windows.FrameworkElement
System.Windows.Controls.Control
System.Windows.Controls.ContentControl
System.Windows.Controls.Primitives.ButtonBase
System.Windows.Controls.Button
实例:
<Button x:Name="btnHelloWorld" Width="160" Height="60" Foreground="Red" Background="Blue" Content="Hello World!" Click="btnHelloWorld_Click"></Button>
x:Name很重要的属性,控件在xaml的x命名空间中的名字,不能重名,是可选的。
Click后面的是处理该事件的方法的名称。
相似的控件:RepeatButton,RepeatButton处于按下状态时不断触发Click事件。ToggleButton,ToggleButton跟CheckBox相似,显示状态的,可设置显示两种或三种状态。
3.TextBox:
继承层次:
System.Object
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Media.Visual
System.Windows.UIElement
System.Windows.FrameworkElement
System.Windows.Controls.Control
System.Windows.Controls.Primitives.TextBoxBase
System.Windows.Controls.TextBox
跟winform中的TextBox相似,不多介绍。
相似的控件:WatermarkedTextBox,WatermarkedTextBox控件有个Watermark属性,当该WatermarkedTextBox的Content为空时,显示Watermark的值,获得焦点后Watermark消失,再失去焦点如果WatermarkedTextBox的Content为空,又显示Watermark。也就是Watermark起提示作用。
还有一些其他控件:CheckBox,RadioButton,ToolTip,ListBox,Image,HyperlinkButton等等,在后来综合示例中会用到,在此不详细介绍。具体使用方法可参考MSDN。
浙公网安备 33010602011771号