从静态对象分配控件时,图像不显示

时间:2016-11-27 17:38:24

标签: c# image winforms user-controls

我正在使用WinForms在C#中制作纸牌游戏。我遇到的问题阻碍了我前进,因为我想添加用户自制的内容。目标是从声明的任何资源/文件动态地将图像设置到卡。我们来看看我的代码。

以下是ExpansionInit.cs

public static Card CopperCard = new Card
{
    Name = "Copper",
    ID = 0,
    BuyValue = 0,
    Type = CardType.Treasure,
    TreasureValue = 1,
    StackCount = GetCardStack(0),
};

以下是GameGUI.cs

using System;
using System.Windows.Forms;
using Dominion.Expansions.Base;
using Dominion.Properties;

namespace Dominion
{
    public partial class GameGui : Form
    {

        public GameGui()
        {
            Game.StartGame();
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //new HandGUI().Show();
            cardCopper = ExpansionInit.CopperCard;
            cardCopper.BackgroundImage = Resources._0_copper;
        }
    }
}

这是Card.cs。我不太确定是否包含这部分很重要,但如果你能在其中找到可能导致问题的内容

using System.Windows.Forms;
using Newtonsoft.Json;

namespace Dominion.Cards
{
    public delegate void CardActivationHandler(Card c, Player p);
    [JsonObject(MemberSerialization.OptIn)]
    public sealed partial class Card : UserControl
    {
        // ReSharper disable once InconsistentNaming
        [JsonProperty]
        public int StackCount;
        [JsonProperty]
        public int ID;
        [JsonProperty]
        public int TreasureValue;
        [JsonProperty]
        public int VictoryValue;
        [JsonProperty]
        public int BuyValue;
        [JsonProperty]
        public CardType Type;
        [JsonProperty]
        public CardActivationHandler Handler;

        [JsonProperty]
        public new string Text { get; set; }

        [JsonProperty]
        public new string Name { get; set; }
        public Card()
        {
            InitializeComponent();
        }
    }

    public enum CardType
    {
        Victory, Treasure, Action, Other
    }
}

1 个答案:

答案 0 :(得分:0)

需要读取对象form1.Controls.Add(cardCopper)并重置位置。