XAML的威力和局限(1)--学习笔记
一般情况下sliverligth程序是代码和xaml的组合. 一般情况下xaml用于布局,代码则用于处理所有用户输入的事件以及所有控件产生的事件,这些控件事件是用户输入事件的处理结果。我们通常认为xaml中的大多数对象的创建和初始化实在页面类(Page)或窗口类(window)的构造函数中完成,导致认为xaml只是应用程序的一小部分,实际上xaml远超过与对象初始化相关的功能,其实之一特性就是数据绑定,无需任何显式的事件处理函数就可以自动更新,甚至整个动画都可以在xaml中定义。
xaml中使用任何常规的函数都无法执行算法,也不能动态地创建对象。xaml中实例化类或结构体必须被一定为公共的,类或结构体必有一个无参的构造函数,而且不能传递参数给构造函数。xaml中可以将一个特殊事件与一个事件处理函数相关联,事件处理程序只能通过代码实现。我们可以只编写代码来卡法几乎所有的应用程序,那什么情况下必须在xaml中完成呢? 那就是模板的构造。下面我们来用代码讲解:
TextBlock的xaml版本
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Height="30" HorizontalAlignment="Left" Margin="39,45,0,0" Name="textBlock1 Text="hi xiucai" VerticalAlignment="Top" />
</Grid>
现在我们在手指触摸屏幕时用c#代码动态创建一个TextBlock
Random r = new Random();
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
{
TextBlock newtextblock = new TextBlock();
newtextblock.Text = "hello c# create newtextblock";
newtextblock.HorizontalAlignment = HorizontalAlignment.Center;
newtextblock.VerticalAlignment = VerticalAlignment.Center;
newtextblock.Margin = new Thickness((ContentPanel.ActualWidth - textBlock1.ActualWidth) *r.NextDouble(),(ContentPanel.ActualHeight - textBlock1.ActualHeight) * r.NextDouble(), 0, 0);
ContentPanel.Children.Add(newtextblock);
e.Complete();
e.Handled = true;
base.OnManipulationStarted(e);
}
两段代码进行比较代码中名为Childern属性实际为一个集合,将textblock添加到Children集合中要显示实现,而xaml中没有,为什么呢?我们先来了解sliverlight中一个重要特性 属性继承(property inheritance)
<phone:PhoneApplicationPage FontSize="Italic">
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Height="30" HorizontalAlignment="Left" Margin="39,45,0,0" Name="textBlock1 Text="hi xiucai" VerticalAlignment="Top" />
</Grid>
</phone:PhoneApplicationPage>
运行结果:hi xiucai 显示为斜体。<phone:PhoneApplicationPage>标签中设置的属性会影响到该页面中所有的Textblock元素,这就被称为属性继承,注意属性继承比传统的简单的父子传递成熟。继承属性比样式属性级别低,优先于默认值。上面的xaml代码还这样写
<TextBlock Text="hi xiucai">
<TextBlock.FontSize>
36
</TextBlock.FontSize>
</TextBlock>
这也是xaml的一个重要语法属性元素(用xml元素表示一个.net属性)注意:属性元素中出现任何东西都是非法。
下面我们来给下代码减肥:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.Background>
<LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Offset="0" Color="Blue"/>
<GradientStop Offset="0.5" Color="White"/>
<GradientStop Offset="2" Color="Red"/>
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="HI XIUCAI" FontFamily="Arial Black"
FontSize="72"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock.Foreground>
<RadialGradientBrush>
<RadialGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Offset="0" Color="Transparent"/>
<GradientStop Offset="1" Color="Red"/>
</GradientStopCollection>
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</TextBlock.Foreground>
</TextBlock>
</Grid>
瘦身原则:
1,集合中至少有一个子项就可以出去集合本身的标签
2,xaml中很多类具有contentproperty特性与.net中的特性有一定的区别,.net中他指与一个类或某个类得成员相关联的一些附加信息。
GradientBrush类中派生LinearGradintBrush和RadialGradientBrush
[ContentPropertyAttribute(“GradientStops”,true)]
public abstract class GradientBrush:Brush
表明类的一个属性是被认为该类的内容,并且不需要该属性元素的标签
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.Background>
<LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
<GradientStop Offset="0" Color="Blue"/>
<GradientStop Offset="0.5" Color="White"/>
<GradientStop Offset="2" Color="Red"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="HI XIUCAI" FontFamily="Arial Black"
FontSize="72"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock.Foreground>
<RadialGradientBrush>
<GradientStop Offset="0" Color="Transparent"/>
<GradientStop Offset="1" Color="Red"/>
</RadialGradientBrush>
</TextBlock.Foreground>
</TextBlock>
</Grid>
运行效果一样。
现在大家明白上面的为什么了吧!
请看下面xaml代码正确吗?
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="1" Width="100" Height="200" VerticalAlignment="Top" HorizontalAlignment="Center"></TextBlock>
<Grid.Background>
<LinearGradientBrush>
<GradientStop Offset="0" Color="Red"></GradientStop>
<GradientStop Offset="1" Color="Beige"></GradientStop>
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="2" Width="100" Height="200" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
</Grid>
代码在vs里面显示错误,因为Childern属性被设置了两次.
错误地方请指正,谢谢!
明天继续学习 资源集合 共享话刷,样式,主题
未完待续……
学习书籍 windows phone7程序设计

浙公网安备 33010602011771号