【万里征程——Windows App开发】控件大集合1

加入控件的方式有多种。大家更喜欢哪一种呢?
1)使用诸如 Blend for Visual Studio 或 Microsoft Visual Studio XAML 设计器的设计工具。
2)在 Visual Studio XAML 编辑器中将控件加入到 XAML 标记中。
3)在代码中加入控件。 当应用执行时会看到你在代码中加入的控件。但在 Visual Studio XAML 设计器中看不到。

前面我们已经用过了Grid、Button等控件,如今就来系统地看看关于控件的一些属性、事件等等。

毫无疑问第一步就是要来加入控件,那么加入控件有哪几种方式呢。

前面我们都是直接在XAML中写的控件或者从工具箱中拖拽出来。

事实上还有2种,一种比較复杂但我们以后也会用到,那就是在C#后台代码中加入控件;还有一种就是在Blend for Visual Studio中拖拽控件了。

后者的功能也很强大,比方要使用动画之类的,这个设计器就能发挥作用了。

控件的属性相比大家都已经会用了,一来能够直接在XAML中加入属性,二来能够在属性视图中加入和改动属性。

这里写图片描写叙述

假设要加入和改动事件呢,相同在属性视图中,点击右上角的闪电图标就可以。假设要加入Click事件,那么在Click的输入框中输入好事件名称后直接按Enter就可以。此时VS就会自己主动跳转到C#后台代码中。第一个參数sender是对处理程序所附加的对象的应用。第二參数是事件数据,它通常在签名中显示为e參数。

private void btnSetStyle_Click(object sender, RoutedEventArgs e)
{
    Button b = (Button)sender;
    b.Height = 400;
    b.Width = 320;
}

上面的这段代码这会将所点击的Button的高设置为400,宽设置为320;除了这样的方式外,也能够按例如以下操作,当中btnSetStyle是当前Button的名字:

private void btnSetStyle_Click(object sender, RoutedEventArgs e)
{
    btnSetStyle.Height = 400;
    btnSetStyle.Width = 320;
}

除此之外,我们也能够不在XAML中定义Click事件,依照例如以下操作也能够达到相同的效果,它会将两个事件相互关联。

public MainPage()
{
     this.InitializeComponent();

     btnSetStyle.Click += new RoutedEventHandler(btnSetStyle_Click);
}

private void btnSetStyle_Click(object sender, RoutedEventArgs e)
{
    btnSetStyle.Height = 400;
    btnSetStyle.Width = 320;
}

前面我们已经了解了假设加入控件、加入/改动属性、加入/改动事件。也了解一下控件的样式。尽管说到样式大家想到的可能是css。想必大家都玩过2048吧,游戏中有很多很多的方格,那么这些方格的样式会不会一个一个去定义呢,当然不是啦,能够直接用样式资源来定位到全部的Button。后面我们也会来实践一下怎样写一个2048小游戏的。

下面是我写的2048里面的样式啦,

<Page.Resources>
   <Style TargetType="Button">
      <Setter Property="FontWeight" Value="Bold"/>
      <Setter Property="FontSize" Value="40"/>
      <Setter Property="HorizontalAlignment" Value="Center"></Setter>
      <Setter Property="VerticalAlignment" Value="Center"></Setter>
      <Setter Property="Background" Value="Gray"></Setter>
      <Setter Property="Width" Value="100"></Setter>
      <Setter Property="Height" Value="100"></Setter>
      <Setter Property="Template">
          <Setter.Value>
              <ControlTemplate TargetType="Button">
                  <Grid x:Name="Grid" Background="Transparent">
                      <Border x:Name="Border" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}" >
                          <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="Center"  VerticalAlignment="Center"/>
                       </Border>
                   </Grid>
               </ControlTemplate>
           </Setter.Value>
       </Setter>
    </Style>
</Page.Resources>

可是这里也有一个问题,假设我们有10个Button控件,却仅仅想当中8个用到这些定义。另外2个想用还有一种控件,那该怎么办呢?

将样式定义为资源。事实上是有2中方式的。


一种就是直接用Style的TargetType属性来定义到全部的目标控件。
还有一种则除了用TargetType属性外。还能够用x:key属性,然后再详细的控件中庸显式的关键字StaticResource来设置详细的Style属性。

<Page.Resources>     
     <Style TargetType="Button">              
         <Setter Property="FontStyle" Value="Oblique" />
         <Setter Property="FontSize" Value="20" />
         <Setter Property="BorderBrush" Value="Green" />
         <Setter Property="BorderThickness" Value="5" />
         <Setter Property="Foreground" Value="Orange" />
         <Setter Property="Height" Value="80"/>
         <Setter Property="Width" Value="160"/>
     </Style>

     <Style x:Key="OtherStyle" TargetType="Button">
         <Setter Property="FontStyle" Value="Italic" />
         <Setter Property="FontSize" Value="16" />
         <Setter Property="Foreground" Value="Lavender" />
         <Setter Property="Height" Value="160"/>
         <Setter Property="Width" Value="320"/>
         <Setter Property="Opacity" Value="0.2"/>
     </Style>                                                            
</Page.Resources>

详细效果见下图,当中Opacity属性为透明度。

这里写图片描写叙述

大家都知道类能够继承,样式也是能够继承的。

尽管这篇博客内容比較少,但更精彩的内容还在后面呢。感谢大家的支持!



感谢您的訪问,希望对您有所帮助。

欢迎大家关注或收藏、评论或点赞。


为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp


posted @ 2017-07-10 13:04  claireyuancy  阅读(207)  评论(0编辑  收藏  举报