原文:《Programming WPF》翻译 第5章 1.不使用样式

作为一个样式如何使其在WPF使用的例子,,让我们看一下TTT简单的实现,如示例5-1。

示例5-1

<!-- Window1.xaml -->

<Window

    x:Class="TicTacToe.Window1"

    xmlns="http://schemas.microsoft.com/winfx/avalon/2005"

    xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"

    Text="TicTacToe">

  <!-- the black background lets the tic-tac-toe -->

  <!-- crosshatch come through on the margins -->

  <Grid Background="Black">

    <Grid.RowDefinitions>

      <RowDefinition />

      <RowDefinition />

      <RowDefinition />

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

      <ColumnDefinition />

      <ColumnDefinition />

      <ColumnDefinition />

    </Grid.ColumnDefinitions>

    <Button Margin="0,0,2,2" Grid.Row="0" Grid.Column="0" x:Name="cell00" />

    <Button Margin="2,0,2,2" Grid.Row="0" Grid.Column="1" x:Name="cell01" />

    <Button Margin="2,0,0,2" Grid.Row="0" Grid.Column="2" x:Name="cell02" />

    <Button Margin="0,2,2,2" Grid.Row="1" Grid.Column="0" x:Name="cell10" />

    <Button Margin="2,2,2,2" Grid.Row="1" Grid.Column="1" x:Name="cell11" />

    <Button Margin="2,2,0,2" Grid.Row="1" Grid.Column="2" x:Name="cell12" />

    <Button Margin="0,2,2,0" Grid.Row="2" Grid.Column="0" x:Name="cell20" />

    <Button Margin="2,2,2,0" Grid.Row="2" Grid.Column="1" x:Name="cell21" />

    <Button Margin="2,2,0,0" Grid.Row="2" Grid.Column="2" x:Name="cell22" />

  </Grid>

</Window>

这个grid的外观上排列了一组9个按钮在一个3X3栅格的TTT单元中,在按钮上使用了页面空白为了TTT的交叉线阴影。对游戏逻辑的一个简单的实现,在xaml后台代码中,如示例5-2所示。

示例5-2

// Window1.xaml.cs



namespace TicTacToe {

  public partial class Window1 : Window {

    // Track the current player (X or O)

    string currentPlayer;



    // Track the list of cells for finding a winner etc.

    Button[] cells;



    public Window1( ) {

      InitializeComponent( );



      // Cache the list of buttons and handle their clicks

      this.cells = new Button[] { this.cell00, this.cell01,  };

      foreach( Button cell in this.cells ) {

        cell.Click += cell_Click;

      }



      // Initialize a new game

      NewGame( );

    }



    // Wrapper around the current player for future expansion,

    // e.g. updating status text with the current player

    string CurrentPlayer {

      get { return this.currentPlayer; }

      set { this.currentPlayer = value; }

    }



    // Use the buttons to track game state

    void NewGame( ) {



      foreach( Button cell in this.cells ) {

        cell.Content = null;

      }

      CurrentPlayer = "X";

    }



    void cell_Click(object sender, RoutedEventArgs e) {

      Button button = (Button)sender;



      // Don't let multiple clicks change the player for a cell

      if( button.Content != null ) { return; }





      // Set button content

      button.Content = CurrentPlayer;



      // Check for winner or a tie

      if( HasWon(this.currentPlayer) ) {

        MessageBox.Show("Winner!", "Game Over");

        NewGame( );

        return;

      }

      else if( TieGame( ) ) {

        MessageBox.Show("No Winner!", "Game Over");

        NewGame( );

        return;

      }





      // Switch player

      if( CurrentPlayer == "X" ) {

        CurrentPlayer = "O";

      }

      else {

        CurrentPlayer = "X";

      }

    }



    // Use this.cells to find a winner or a tie

    bool HasWon(string player) {}

    bool TieGame( ) {}

  }

}

我们的简单TTT逻辑使用字符串代表玩家,使用按钮来跟踪游戏状态。当点击任意一个按钮时,我们将内容设置为字符串,用来象征当前玩家以及转换玩家。当游戏结束的时候,每一个按钮上的内容都会被清除。游戏中的截图如图5-1。

图5-1





注意到图5-1中,grid的背景来自页面的空白。这些空白差不多使grid看上去像一个可绘制的TTT木板(虽然我们将来会做的更好)。然而,如果我们真的指望模仿一个手绘的游戏,我们已经对按钮上的字体大小做了设置,但并没匹配到线条的厚度。

一种修复这个问题的方法是为每一个按钮对象设置字体和宽度,如示例5-3。

示例5-3

<Button FontSize="32" FontWeight="Bold"  x:Name="cell00" />

<Button FontSize="32" FontWeight="Bold" x:Name="cell01" />

<Button FontSize="32" FontWeight="Bold" x:Name="cell02" />

<Button FontSize="32" FontWeight="Bold" x:Name="cell10" />

<Button FontSize="32" FontWeight="Bold" x:Name="cell11" />

<Button FontSize="32" FontWeight="Bold" x:Name="cell12" />

<Button FontSize="32" FontWeight="Bold" x:Name="cell20" />

<Button FontSize="32" FontWeight="Bold" x:Name="cell21" />

<Button FontSize="32" FontWeight="Bold" x:Name="cell22" />

依照我的视觉敏感性,今天,虽然这样做使得X的和O的外观更好,一旦我以后想改动它,我就要负责在9个独立的地方改变这些属性,这是重复性的努力——违反了我的编码敏感性。我宁愿重制我的决定——为了以后的维护,将我的TTT单元的外观放在一个共同的地方。这是样式派得上用场的地方。

《Programming WPF》翻译 第5章 1.不使用样式的更多相关文章

  1. 《Programming WPF》翻译 第6章 2.资源与样式

    原文:<Programming WPF>翻译 第6章 2.资源与样式 WPF的样式机制以来于资源体系来定位样式.正如你在第5章看到的,样式在元素的资源片段中定义,而且样式通过其名字被引用, ...

  2. 《Programming WPF》翻译 第5章 4.元素类型样式

    原文:<Programming WPF>翻译 第5章 4.元素类型样式 命名样式非常有用,当你得到一组属性并应用到特点的元素上.然而,如果你想要应用一个统一的样式到所有确定元素类型的实例, ...

  3. 《Programming WPF》翻译 第5章 2.内嵌样式

    原文:<Programming WPF>翻译 第5章 2.内嵌样式 每一个“可样式化”的WPF元素都有一个Style属性,可以在内部设置这个属性--使用XAML属性-元素的语法(在第一章讨 ...

  4. 《Programming WPF》翻译 第9章 5.默认可视化

    原文:<Programming WPF>翻译 第9章 5.默认可视化 虽然为控件提供一个自定义外观的能力是有用的,开发者应该能够使用一个控件而不用必须提供自定义可视化.这个控件应该正好工作 ...

  5. 《Programming WPF》翻译 第9章 6.我们进行到哪里了?

    原文:<Programming WPF>翻译 第9章 6.我们进行到哪里了? 只有当任何内嵌控件都没有提供你需要的底层行为时,你将要写一个自定义控件.当你写一个自定义控件,你将要使用到依赖 ...

  6. 《Programming WPF》翻译 第9章 4.模板

    原文:<Programming WPF>翻译 第9章 4.模板 对一个自定义元素最后的设计考虑是,它是如何连接其可视化的.如果一个元素直接从FrameworkElement中派生,这将会适 ...

  7. 《Programming WPF》翻译 第9章 3.自定义功能

    原文:<Programming WPF>翻译 第9章 3.自定义功能 一旦你挑选好一个基类,你将要为你的控件设计一个API.大部分WPF元素提供属性暴露了多数功能,事件,命令,因为他们从框 ...

  8. 《Programming WPF》翻译 第9章 2.选择一个基类

    原文:<Programming WPF>翻译 第9章 2.选择一个基类 WPF提供了很多类,当创建一个自定义元素时,你可以从这些类中派生.图9-1显示了一组可能作为类--可能是合适的基类, ...

  9. 《Programming WPF》翻译 第9章 1.自定义控件基础

    原文:<Programming WPF>翻译 第9章 1.自定义控件基础 在写一个自定义控件之前,你需要问的第一个问题是,我真的需要一个自定义控件吗?一个写自定义控件的主要原因是为了用户界 ...

随机推荐

  1. AsyncCTP &IdentityModel

    AsyncCTP.0.3.nupkg http://pan.baidu.com/s/1kVhPaYj Microsoft.IdentityModel.6.1.7600.16394  http://pa ...

  2. android 无法生成R文件的原因剖析

    android 无法生成R文件 是件痛苦的事情,即使有时候你xml文件没有错误,他都无法生成.针对此问题,我总结以下几个方面的原因. 一.xml本身有错误 R.java这个文件是会自动生成的,但是如果 ...

  3. JAVA在线基础教程!

    http://www.runoob.com/java/java-tutorial.html http://www.51zxw.net/list.aspx?cid=380 http://www.weix ...

  4. Android 状态栏通知Notification、NotificationManager简介

    Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...

  5. Jquery回车键切换焦点方法(兼容各大浏览器)

    做项目时,客户要求能够用enter回车直接切换输入(焦点),当最后一个时候,直接提交信息. 第一想法就是,网上去copy一段代码直接用.但了百度.谷歌找了个遍,找到的代码80%以上都是一样的.有的代码 ...

  6. poj 2184 Cow Exhibition(dp之01背包变形)

    Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...

  7. [WebStrom] Cannot detect file change to trigger webpack re-compile

    Working with editors/IDEs supporting “safe write” Note that many editors support “safe write” featur ...

  8. [Angular 2] Using events and refs

    This lesson shows you how set listen for click events using the (click) syntax. It also covers getti ...

  9. Qt 图形特效(Graphics Effect)介绍

    原文链接:Qt 图形特效(Graphics Effect)介绍 QGraphicsEffect也是Qt-4.6引入的一个新功能.它让给图形元素QGraphicsItem增加更佳视觉效果的编程变得非常简 ...

  10. TableView数据源方法的执行顺序

    UITableView显示数据的过程 1.调用一次tableView:numberOfRowsInSection:方法确定行数 2.调用多次tableView:heightForRowAtIndexP ...