1

現在、WPFで作成しているダイスゲームのカスタムボタンコントロールを作成しようとしています。ダイの値が変更されても、実行中のUIの画像が変更されないことを除いて、すべてが正しく機能しています。デバッグセッションを通じて、イメージソースが期待どおりに変更され、すべての値が期待どおりに変更されていることを確認できますが、イメージは更新されたくないようです。私は多くの同様の質問を調べてみましたが、どの質問も私の特定の状況には当てはまらないようです。私は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();           
        }    
    }
}
4

1 に答える 1

0

画像を変更するたびに、以下を実行してみてください= new BitmapImage(new Uri(.. ..

私はそれが通常機能することを読みました、またはあなたはここで議論された解決策に従うことができます http://social.msdn.microsoft.com/Forums/en/wpf/thread/976e8d89-aafd-4708-9e4f-87655a5da558

于 2013-02-28T18:55:13.827 に答える