簡體   English   中英

圖像不會在代碼中更新,只有從 WPF 中的按鈕繼承的自定義控件

[英]Image will not update in code only custom control inheriting from button in WPF

我目前正在嘗試為我在 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();           
        }    
    }
}

每次您想更改圖像時,請嘗試執行以下操作 = new BitmapImage(new Uri(...

我已經閱讀了通常有效的方法,或者您可以按照此處討論的解決方案http://social.msdn.microsoft.com/Forums/en/wpf/thread/976e8d89-aafd-4708-9e4f-87655a5da558

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM