码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-11-13 18:21:42      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:

原文:《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.不使用样式

标签:

原文地址:http://www.cnblogs.com/lonelyxmas/p/4962862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!