天天看點

WPF編遊戲系列 之四 使用者控件

原文: WPF編遊戲系列 之四 使用者控件        在上一篇 《WPF編遊戲系列 之三 物品清單》 中,對物品清單進行了示範,其中反複用到了同一組控件(如下圖),而且 顔昌鋼 也指出在3.2.2中使用的C#代碼過多,其實我在寫這些代碼時也有同感,的确很繁瑣也不好維護。其實這組控件的結構就是:<StackPanel><Image><Textblock><Textblock><Image>這5個控件的組合,那麼能否将其做成一個控件組呢?“使用者控件”即可實作這個功能。

WPF編遊戲系列 之四 使用者控件
1. 建立一個使用者控件,右鍵項目名稱,Add->New Item,選擇User Control,建立一個GoodsElement控件。
WPF編遊戲系列 之四 使用者控件
VS會在Solution Explorer中建立一個新的XAML檔案。
WPF編遊戲系列 之四 使用者控件
2. 控件建立好,就要對它進行編輯了,打開GoodsElement.xaml,将之前重複使用的控件全部定義到這裡。

<UserControl x:Class="XMarket.GoodsElement"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="195" Width="150">
    <Grid>
       <Border BorderThickness="3" CornerRadius ="5"
               Background="#FFFFCC" BorderBrush="#FF6633">
          <StackPanel Orientation="Vertical" Margin="5"
HorizontalAlignment="Center">
              <Image Name="goodsImage" Height="80" Width="80" Margin="5"></Image>
              <TextBlock Name="goodsPrice" Margin="5"></TextBlock>
              <TextBlock Name="goodsQty" Margin="5"></TextBlock>
              <Image Name="goodsBuy" Source="image/add.png" 
Height="25" Width="25" Cursor="Hand" Margin="5">
                  <Image.ToolTip>Add Quantity</Image.ToolTip>
              </Image>
          </StackPanel>
       </Border>
    </Grid>
</UserControl>      

效果圖:

WPF編遊戲系列 之四 使用者控件

3. 控件編好後,回到上一篇的3.2.2 将那些C#修改一下,代碼行數減少了一半還多。這樣做雖然C#少了,不知道程式的效率會不會提高?

int rowNum = 0;
int colNum = 0;
//Make element for goods
for (int i = 0; i < num; i++)
{
  //使用剛定義的GoodsElement控件
GoodsElement goods = new GoodsElement();
  BitmapImage bitImage = new BitmapImage();
  bitImage.BeginInit();
  bitImage.UriSource = new Uri("image/shop/" + res[i, 2].ToString(), UriKind.Relative);
  bitImage.EndInit();
  
  goods.goodsImage.Source = bitImage;
  goods.goodsPrice.Text = "Price:  $" + res[i, 6].ToString();
  goods.goodsQty.Text = "Quantity:  " + res[i, 5].ToString();
                    
  goods.goodsQty.Name = "gQty" + res[i, 0].ToString();
  object findTextObj = queryGrid.FindName("gQty" + res[i, 0].ToString());
  if (findTextObj != null)
  {
    queryGrid.UnregisterName("gQty" + res[i, 0].ToString());
  }
  queryGrid.RegisterName(goods.goodsQty.Name, goods.goodsQty);
                    
  goods.goodsBuy.Tag = res[i, 0].ToString() + "-" + res[i, 5].ToString() + "-" + res[i, 6].ToString();
  goods.goodsBuy.MouseLeftButtonDown += addImage_MouseLeftButtonDown;
                    
  goods.goodsBuy.Name = "bImage" + res[i, 0].ToString();
  object findImageObj = queryGrid.FindName("bImage" + res[i, 0].ToString());
  if (findImageObj != null)
  {
    queryGrid.UnregisterName("bImage" + res[i, 0].ToString());
  }
  queryGrid.RegisterName(goods.goodsBuy.Name, goods.goodsBuy);

  //Set GoodsElement grid postion
  if (colNum == 5)
  {
    rowNum++;
    colNum = 0;
  }
  goods.SetValue(Grid.RowProperty, rowNum);
  goods.SetValue(Grid.ColumnProperty, colNum);
  colNum++;
  queryGrid.Children.Add(goods);
}      

待續… …