'对象'不包含'值'

时间:2016-01-22 16:23:24

标签: c#

我正在构建一个函数来减少18个按钮的相同代码。

以下是代码:

void hexagon_Click(object sender, ActionEventArgs e)
    {
        count++;
        this.Dispatcher.Invoke((Action)(() =>
        {
            sender.RectOne.Fill = sender.Brush;
            sender.RectTwo.Fill = sender.Brush;
            sender.RectThree.Fill = sender.Brush;
        }));
        if (count == 1)
        {
            kliknatopole = sender;
        }
        else if (count == 2)
        {
            if (kliknatopole == sender)
            {
                return;
            }
            else
            {
                if (kliknatopole.Brush == sender.brush)
                {
                    levelUpControUI(level);
                    prepareForNextLevel();
                    nextLevel1();
                    count = 0;
                    sender.Enabled = false;
                    kliknatopole.Enabled = false;
                }
                else
                {
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        sender.RectOne.Fill = sender.Brush;
                        sender.RectTwo.Fill = sender.Brush;
                        sender.RectThree.Fill = sender.Brush;
                    }));
                    Thread.Sleep(500);
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        sender.RectOne.Fill = Brushes.Transparent;
                        sender.RectTwo.Fill = Brushes.Transparent;
                        sender.RectThree.Fill = Brushes.Transparent;
                        kliknatopole.RectOne.Fill = Brushes.Transparent;
                        kliknatopole.RectTwo.Fill = Brushes.Transparent;
                        kliknatopole.RectThree.Fill = Brushes.Transparent;
                    }));
                    toLevelOne();
                    count = 0;
                }
            }
        }
    }

现在我不知道为什么,kliknatopole private objectsender无法访问班级HexagonControl中的值,这是:

 public partial class HexagonControl : UserControl
{
    public BaseControlLogic controlLogic { get; set; }
    public HexagonControl()
    {
        InitializeComponent();
        controlLogic = new BaseControlLogic();
    }

    public Rectangle RectOne { get { return rectOne; } }
    public Rectangle RectTwo { get { return rectTwo; } }
    public Rectangle RectThree { get { return rectThree; } }
    public Brush Brush { set; get; }
}

对于RectOne,Two and Three以及Brush,我收到此错误:

'object' does not contain a definition for 'RectOne' and no extension method 'RectOne' accepting a first argument of of type 'object' could be found

我把HexagonControl公之于众,但那并没有解决它。我还尝试创建object并传递sender值,但仍然没有解决它,它只是给了我Cannot implicitly convert type 'object' to 'LongName.HexagonControl'. An explicit conversion exists

可能导致此错误的原因,以及如何解决此问题。

2 个答案:

答案 0 :(得分:2)

只需将sender投射到原始类型:

((HexagonControl)sender).RectOne.Fill = ((HexagonControl)sender).Brush;

当然,你必须为其他8个用法做到这一点

答案 1 :(得分:2)

您必须将sender转换为HexagonControl类型,或将其值发送给另一个HexaControl类型变量。请尝试以下方法:

void hexagon_Click(object sender, ActionEventArgs e)
{
    if(sender.GetType() != typeof(HexagonControl))
            return;
    count++;
    HexagonControl realSender = (HexagonControl) sender;

    [...]

在方法的其余部分使用realSender代替sender。这样一来,如果Exception不是sender,就会阻止HexagonControl,只是为了预防。

希望它有所帮助!