我目前正在尝试为我在 WPF 中编写的骰子游戏构建自定义按钮控件。一切正常,除了运行时 UI 中的图像在 die 值更改时不会更改。我可以通过我的调试会话确认图像源正在按预期更改,并且所有值都按预期更改,但图像似乎不想更新。我试过查看许多类似的问题,但似乎没有一个问题适用于我的特定情况。我是 WPF 的新手,不太确定我在这里做错了什么……我当前的代码如下:
using BluffersDice.GameEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace BluffersDice.Interface.CustomControls
{
public class DieButton : Button
{
private Die _DieValue;
private readonly BitmapImage ONE_IMAGE = new BitmapImage(new Uri("/res/dice/1.png",UriKind.Relative));
private readonly BitmapImage TWO_IMAGE = new BitmapImage(new Uri("/res/dice/2.png", UriKind.Relative));
private readonly BitmapImage THREE_IMAGE = new BitmapImage(new Uri("/res/dice/3.png", UriKind.Relative));
private readonly BitmapImage FOUR_IMAGE = new BitmapImage(new Uri("/res/dice/4.png", UriKind.Relative));
private readonly BitmapImage FIVE_IMAGE = new BitmapImage(new Uri("/res/dice/5.png", UriKind.Relative));
private readonly BitmapImage SIX_IMAGE = new BitmapImage(new Uri("/res/dice/6.png", UriKind.Relative));
private const string HELD_LABEL_TEXT = "Held";
//private bool initcompleted = false;
private Label HoldLabel { get; set; }
public DieButton() : this(new Die())
{}
public DieButton(Die dieValue)
{
Background = Brushes.Transparent;
BorderBrush = new SolidColorBrush(Colors.Transparent);
BorderThickness = new Thickness(6);
HoldLabel = new Label() { MinHeight = 15 };
Click += This_OnClick;
DieValueChanged += DieValueChangedHandler;
dieValue.IsHeldChanged += DieValue_IsHeldChanged;
dieValue.DieValueChanged += DieValueChangedHandler;
_DieValue = dieValue;
Panel = new StackPanel()
{
Orientation = Orientation.Vertical,
Margin = new Thickness(8)
};
DieImage = new Image() { Source = GetDieImageSource() };
Panel.Children.Add(DieImage);
Panel.Children.Add(HoldLabel);
Content = Panel;
UpdateButtonContent();
//initcompleted = true;
}
private void This_OnClick(object sender, RoutedEventArgs e)
{
DieValue.ToggleHold();
}
public event EventHandler DieValueChanged;
public Die DieValue
{
get
{
return _DieValue;
}
set
{
_DieValue = value;
if (DieValueChanged != null)
{
DieValueChanged(this, new EventArgs());
}
}
}
private Image DieImage { get; set; }
private StackPanel Panel { get; set; }
private void DieValue_IsHeldChanged(object sender, EventArgs e)
{
var d = (Die)sender;
if (d.IsHeld)
{
BorderBrush = new SolidColorBrush(Colors.Yellow);
}
else
{
BorderBrush = new SolidColorBrush(Colors.Transparent);
}
HoldLabel.Content = DieValue.IsHeld ? HELD_LABEL_TEXT : string.Empty;
}
private void DieValueChangedHandler(object sender, EventArgs e)
{
DieImage.Source = GetDieImageSource();
UpdateButtonContent();
}
private ImageSource GetDieImageSource()
{
switch (DieValue.Value)
{
case 1:
return ONE_IMAGE;
case 2:
return TWO_IMAGE;
case 3:
return THREE_IMAGE;
case 4:
return FOUR_IMAGE;
case 5:
return FIVE_IMAGE;
case 6:
return SIX_IMAGE;
default:
return null;
}
}
private void UpdateButtonContent()
{
(Panel.Children[0] as Image).Source = GetDieImageSource();
}
}
}
窗口正在用于:
using BluffersDice.GameEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BluffersDice.Interface.CustomControls;
namespace BluffersDice.Interface
{
/// <summary>
/// Interaction logic for UserTurn.xaml
/// </summary>
public partial class PlayerTurn : Window
{
public Turn TurnState { get; set; }
private Roll CurrentRoll { get; set; }
public PlayerTurn()
{
CurrentRoll = new Roll();
InitializeComponent();
btn_Die1 = new DieButton(CurrentRoll.Dice[0]);
btn_Die2 = new DieButton(CurrentRoll.Dice[1]);
btn_Die3 = new DieButton(CurrentRoll.Dice[2]);
btn_Die4 = new DieButton(CurrentRoll.Dice[3]);
btn_Die5 = new DieButton(CurrentRoll.Dice[4]);
GameState.Caller.StartNewTurn();
TurnState = GameState.Caller.StartNewTurn();
lbl_PlayerTitle.Text = string.Format(lbl_PlayerTitle.Text, GameState.Caller.Id);
}
private void btn_DieValuechanged(object sender, EventArgs ea)
{
var d = (Die)sender;
MessageBox.Show(String.Format("Die Button {0} Value Changed To {1}", d.Id, d.Value));
}
private void DieValueChanged(object sender, EventArgs e)
{
var d = (Die)sender;
//MessageBox.Show(String.Format("Die {0} Value Changed To {1}", d.Id, d.Value));
}
private void RollDice_btnClick(object sender, RoutedEventArgs e)
{
CurrentRoll.RollDice();
}
}
}