Windows phone中最常用的控件是TextBlock、TextBox和Button。TextBlock用来显示信息,TextBox一般用来输入信息,Button用来相应点击事件。但是不绝对是这样的,为什么呢,因为silverlight最强大的地方在于模板,只要你定义合适的模板,那TextBlock可以作为一个TextBox使用也可以作为一个Button使用。关于模板的内容在以后的章节中描述,现在是要知道,silverlight可以千变万化就好了,windows phone 7SDK是基于silverlight3的,7.1SDK是基于silvelight4的所以windows phone的控件也是千变万化的。
看一下TextBlock的属性、方法与事件
属性
| 名称 | 说明 | |
|---|---|---|
| ActualHeight | FrameworkElement。) | |
| ActualWidth | FrameworkElement。) | |
| AllowDrop | UIElement。) | |
| AreAnyTouchesCaptured | UIElement。) | |
| AreAnyTouchesCapturedWithin | UIElement。) | |
| AreAnyTouchesDirectlyOver | UIElement。) | |
| AreAnyTouchesOver | UIElement。) | |
| Background | Brush。 | |
| BaselineOffset | 获取或设置每个文本行相对于基线的偏移量。 | |
| BindingGroup | FrameworkElement。) | |
| BitmapEffect | 已过时。UIElement。) | |
| BitmapEffectInput | 已过时。UIElement。) | |
| BreakAfter | LineBreakCondition,它指示内容应当如何在当前元素之后进行分行。 | |
| BreakBefore | LineBreakCondition,它指示内容应当如何在当前元素之前进行分行。 | |
| CacheMode | UIElement。) | |
| Clip | UIElement。) | |
| ClipToBounds | UIElement。) | |
| CommandBindings | UIElement。) | |
| ContentEnd | TextPointer。 | |
| ContentStart | TextPointer。 | |
| ContextMenu | FrameworkElement。) | |
| Cursor | FrameworkElement。) | |
| DataContext | FrameworkElement。) | |
| DefaultStyleKey | FrameworkElement。) | |
| DependencyObjectType | DependencyObject。) | |
| DesiredSize | UIElement。) | |
| Dispatcher | DispatcherObject。) | |
| Effect | UIElement。) | |
| FlowDirection | FrameworkElement。) | |
| Focusable | UIElement。) | |
| FocusVisualStyle | FrameworkElement。) | |
| FontFamily | TextBlock 的首选顶级字体系列。 | |
| FontSize | TextBlock 的顶级字号。 | |
| FontStretch | TextBlock 的顶级字体拉伸特征。 | |
| FontStyle | TextBlock 的顶级字形。 | |
| FontWeight | TextBlock 的顶级字体粗细。 | |
| ForceCursor | FrameworkElement。) | |
| Foreground | Brush。 | |
| HasAnimatedProperties | UIElement。) | |
| Height | FrameworkElement。) | |
| HorizontalAlignment | FrameworkElement。) | |
| HostedElementsCore | TextBlock 承载的元素的枚举数。 | |
| InheritanceBehavior | FrameworkElement。) | |
| Inlines | Inline 元素。 | |
| InputBindings | UIElement。) | |
| InputScope | FrameworkElement。) | |
| IsArrangeValid | UIElement。) | |
| IsEnabled | UIElement。) | |
| IsEnabledCore | UIElement。) | |
| IsFocused | UIElement。) | |
| IsHitTestVisible | UIElement。) | |
| IsHyphenationEnabled | 获取或设置一个值,该值指示是否启用文字的自动断字功能。 | |
| IsInitialized | FrameworkElement。) | |
| IsInputMethodEnabled | UIElement。) | |
| IsKeyboardFocused | UIElement。) | |
| IsKeyboardFocusWithin | UIElement。) | |
| IsLoaded | FrameworkElement。) | |
| IsManipulationEnabled | UIElement。) | |
| IsMeasureValid | UIElement。) | |
| IsMouseCaptured | UIElement。) | |
| IsMouseCaptureWithin | UIElement。) | |
| IsMouseDirectlyOver | UIElement。) | |
| IsMouseOver | UIElement。) | |
| IsSealed | DependencyObject。) | |
| IsStylusCaptured | UIElement。) | |
| IsStylusCaptureWithin | UIElement。) | |
| IsStylusDirectlyOver | UIElement。) | |
| IsStylusOver | UIElement。) | |
| IsVisible | UIElement。) | |
| Language | FrameworkElement。) | |
| LayoutTransform | FrameworkElement。) | |
| LineHeight | 获取或设置各行内容的高度。 | |
| LineStackingStrategy | TextBlock 内的每行文本确定行框的机制。 | |
| LogicalChildren | FrameworkElement.LogicalChildren。) | |
| Margin | FrameworkElement。) | |
| MaxHeight | FrameworkElement。) | |
| MaxWidth | FrameworkElement。) | |
| MinHeight | FrameworkElement。) | |
| MinWidth | FrameworkElement。) | |
| Name | FrameworkElement。) | |
| Opacity | UIElement。) | |
| OpacityMask | UIElement。) | |
| OverridesDefaultStyle | FrameworkElement。) | |
| Padding | TextBlock 所显示内容之间的边距的宽度。 | |
| Parent | FrameworkElement。) | |
| PersistId | 已过时。UIElement。) | |
| RenderSize | UIElement。) | |
| RenderTransform | UIElement。) | |
| RenderTransformOrigin | UIElement。) | |
| Resources | FrameworkElement。) | |
| SnapsToDevicePixels | UIElement。) | |
| Style | FrameworkElement。) | |
| StylusPlugIns | UIElement。) | |
| Tag | FrameworkElement。) | |
| TemplatedParent | FrameworkElement。) | |
| Text | TextBlock 的文本内容。 | |
| TextAlignment | 获取或设置一个值,该值指示文本内容的水平对齐方式。 | |
| TextDecorations | TextBlock 的文本的效果。 | |
| TextEffects | 获取或设置要应用于此元素中的文本内容的效果。 | |
| TextTrimming | 获取或设置当内容溢出内容区域时要采取的文本修整行为。 | |
| TextWrapping | TextBlock 对文本进行换行的方式。 | |
| ToolTip | FrameworkElement。) | |
| TouchesCaptured | UIElement。) | |
| TouchesCapturedWithin | UIElement。) | |
| TouchesDirectlyOver | UIElement。) | |
| TouchesOver | UIElement。) | |
| Triggers | FrameworkElement。) | |
| Typography | 获取此元素的内容的当前有效版式变体。 | |
| Uid | UIElement。) | |
| UseLayoutRounding | FrameworkElement。) | |
| VerticalAlignment | FrameworkElement。) | |
| Visibility | UIElement。) | |
| VisualBitmapEffect | 已过时。Visual。) | |
| VisualBitmapEffectInput | 已过时。Visual。) | |
| VisualBitmapScalingMode | Visual。) | |
| VisualCacheMode | Visual。) | |
| VisualChildrenCount | FrameworkElement.VisualChildrenCount。) | |
| VisualClearTypeHint | Visual。) | |
| VisualClip | Visual。) | |
| VisualEdgeMode | Visual。) | |
| VisualEffect | Visual。) | |
| VisualOffset | Visual。) | |
| VisualOpacity | Visual。) | |
| VisualOpacityMask | Visual。) | |
| VisualParent | Visual。) | |
| VisualScrollableAreaClip | Visual。) | |
| VisualTextHintingMode | Visual。) | |
| VisualTextRenderingMode | Visual。) | |
| VisualTransform | Visual。) | |
| VisualXSnappingGuidelines | Visual。) | |
| VisualYSnappingGuidelines | Visual。) | |
| Width | FrameworkElement。) |
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Margin="10 10 10 10" Text="Margin 10 10 10 10" />
<TextBlock Margin="30 30 30 30" Text="Margin 30 30 30 30" />
<TextBlock Margin="50 50 50 50" Text="Margin 50 50 50 50" />
<TextBlock Margin="100 100 100 100" Text="Margin 100 100 100 100"/>
</Grid>
可以看到Margin的四个边距都起到了作用,如果你设置了控件的Height和Width,那么默认只有左边距和上边距起作用,这事因为,一般的控件继承自FrameworkElement类,这个类有对齐属性,也就是VerticalAlignment(垂直对其方式)和HorizontalAlignment(水平对其方式),默认垂对齐方式是向上对齐,水平对齐方式是想做,而Margin的值是依据对齐方式起作用的,多以控件的Width和Height设为固定值时,空间的大小就不会随着Margin的值调整,也就无法做到,上下左右都满足。所以就出现了左和上起作用的现象。但是有一个前提,就是Margin的值不能太大,比如VerticalAlignment=“Top” ,但是如果你的Margin=“10 20 20 1000”就会出问题,Margin=“10 10 10 10”,下边距也是10,无法同时满足上下边距都是10,就会出现top优先,但是过大就会出现布局错误,即使设置了Height属性页没用,控件会被搞到屏幕外面去了,规律是,Margin.left+Margin.Right+控件Width小于父控件的Width+Padding或者Margin.Top+Margin.Bottom+控件Height小于父控件的Height+Padding,按照VerticalAlignment与HorizontalAlignment的设置来决定谁起作用,但是大于的话,就会吧控件挤到屏幕外面或者部分被掩盖
正正常情况下(Margin.left+Margin.Right+控件Width小于父控件的Width+Padding或者Margin.Top+Margin.Bottom+控件Height小于父控件的Height+Padding)对齐方式影响Margin的情况基本如下
VerticalAlignment=“Top” Margin.Top起作用
VerticalAlignment=“Botton”Margin.Bottom起作用
HorizontalAlignment=“Left” Margin.Left起作用
HorizontalAlignment=“Right”Margin.Right起作用
Padding属性和Margin类似,他表示内边距,也就是控件中,子空间与他的距离
如果Margin和Padding都设置了,那就把二者相加
Text属性用来设置显示的内容
其他属性不做介绍了,比如Foreground这种属性,再次出过多介绍没有任何意义,对于初学者来说,首先要掌握的就是空间的显示与布局
事件
Tap事件
Tap事件是当触摸控件时触发的事件,跟点击事件差不多,之所以又这种事件是因为Windows phone手机是触屏的,必须有触摸事件
看如下代码
xaml:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Margin="10 10 10 10" Text="Margin 10 10 10 10" Tap="TextBlock_Tap" ManipulationStarted="TextBlock_ManipulationStarted" ManipulationDelta="TextBlock_ManipulationDelta" ManipulationCompleted="TextBlock_ManipulationCompleted"/>
<TextBlock Margin="30 30 30 30" Text="Margin 30 30 30 30" />
<TextBlock Margin="50 50 50 50" Text="Margin 50 50 50 50" />
<TextBlock Margin="100 100 100 100" Text="Margin 100 100 100 100"/>
</Grid>
c#:
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 Microsoft.Phone.Controls;
namespace Property
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
string infoStr = "";
private void TextBlock_Tap(object sender, GestureEventArgs e)
{
infoStr += "Tap";
}
private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
infoStr += "Start";
}
private void TextBlock_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
infoStr += "Delta";
}
private void TextBlock_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
string location = string.Format("X:{0},Y:{1}",e.ManipulationOrigin.X.ToString(),e.ManipulationOrigin.Y.ToString());
MessageBox.Show(location);
}
}
}
代码中e.ManipulationOrigin.X和e.ManipulationOrigin.Y分别是滑动后和滑动开始时两个点的距离,X表示X方向的巨鹿,Y表示Y方向的距离,他们的值根据您滑动的方向而定,可正可负,具体在以后的章节中介绍,运行程序,在第一个TextBlock控件上滑动一下,效果如下
其他的一些时间和Win Form或者Web Form上类似了,一看事件名称就知道是什么意思,有关某些控件的特有属性和特有事件,在以后的章节中介绍
我新浪微博的昵称是“@马蔬菜”,多多关注,谢谢