简体   繁体   English

C#修复错误:“非静态字段,方法或属性需要对象引用”

[英]C# Fixing error: “An object reference is required for the non-static field, method, or property”

I'm a C#(and programming) newbie. 我是C#(和编程)新手。 I have a windows store app where I want to perform some actions on a countdown timer's time up. 我有一个Windows商店应用程序,我想在其中执行倒数计时器计时的一些操作。 So I tried creating an event like in here and the resulting code is this: 所以我尝试在这里创建一个事件,结果代码如下:

CountDownTimer.cs CountDownTimer.cs

public void timer_Tick(object sender, object e)
{
    if (sw.Elapsed < duration)
    {
        Seconds = (int)(duration - sw.Elapsed).TotalSeconds;
        TimeElapsed = String.Format("{0} second(s)", Seconds);
    }
    else
    {
        TimeElapsed = "Times Up";
        timer.Stop();
        Views.EquationView.OnTimesUp();
    }
}

EquationView.xaml.cs EquationView.xaml.cs

public event System.EventHandler TimesUp;
public void OnTimesUp()
{
    if (TimesUp != null)
    {
        TimesUp(this, EventArgs.Empty);
        submitButton.IsEnabled = true;
        MessageDialog msgDialog = new MessageDialog("Your time's up which counts as a wrong answer. Click start for a new equation");
        msgDialog.ShowAsync();
        //more code
    }
}

But I get the error mentioned in the question title. 但是我得到了问题标题中提到的错误。 Searching around MDSN and SO seems I have two options 搜索MDSN和SO似乎我有两个选择

1) setting OnTimeSUp() to static but then submitBtn.IsEnabled = true; 1) 将OnTimeSUp(设置为静态 ,然后submitBtn.IsEnabled = true; gives me an error. 给我一个错误。 I tried to pass the button as a parameter but it won't do 我试图将按钮作为参数传递,但它不会

2) changing Views.EquationView.OnTimesUp(); 2)更改Views.EquationView.OnTimesUp(); to this.Views.EquationView.OnTimesUp(); this.Views.EquationView.OnTimesUp(); or creating a new instance and then instance.Views.EquationView.OnTimesUp() ; 创建一个新实例 ,然后创建 instance.Views.EquationView.OnTimesUp() ; still problem 仍然有问题

I'd much appreciate any help. 非常感谢您的帮助。 I'm also open to suggestions for more elegant and efficient code. 我也乐于接受有关更优雅,更高效的代码的建议。

--UPDATE-- Thank's to A.Abramov I no longer get errors but besides showing the message dialog and updating the data model(1st line) the method OnTimesUp() does nothing. -更新-感谢A.Abramov,我不再遇到错误,但是除了显示消息对话框和更新数据模型(第一行)之外,方法OnTimesUp()也不起作用。 When invoked by other methods the other lines do work. 当由其他方法调用时,其他行也会起作用。

CountDownTimer.cs CountDownTimer.cs

    public void timer_Tick(object sender, object e)
    {
        if (sw.Elapsed < duration)
        {
           //code
        }
        else
        {
           //THE CHANGE
            new Views.EquationView().OnTimesUp();
        }
    }

EquationView.xaml.cs EquationView.xaml.cs

    public void OnTimesUp()
    {
        App.player.AnsweredWronglyAsync();
        AfterWrongAnswer();
        submitButton.IsEnabled = false;
        commandBarResetButton.IsEnabled = false;
        MessageDialog timesUpMsg = new MessageDialog("Time's up!");
        timesUpMsg.ShowAsync();
    }

Well well well. 好吧好吧。 isn't this a typical OOP problem that should be explained more. 这不是典型的OOP问题,应该进一步解释。 I guess i`ll be the one to do it. 我想我会做的。

In your program you are trying to reach Views.EquationView.OnTimesUp(); 在您的程序中,您尝试到达Views.EquationView.OnTimesUp(); from the CountDownTimer class. 从CountDownTimer类。 The problem is, that Equation view does not have OnTimesUp() for all the objects, it's object specific - so C# is not sure how to activate the function. 问题是,方程式视图没有针对所有对象的OnTimesUp(),而是对象特定的-因此C#不确定如何激活该函数。

The solution is, as you have described - to create an instance of View.EquationView.OnTimesUp() - this way, the function will trigger from within the object . 如您所描述的,解决方案是-创建View.EquationView.OnTimesUp()的实例-这样,该函数将在object中触发。

You say it's problematic, i say we're here to solve problems. 您说这有问题,我说我们是在这里解决问题。 Try this code - if it's wrong, comment on this answer with the compiler message, and we'll see how to fix it :) 尝试使用此代码-如果输入错误,请在编译器消息中对此答案进行评论,然后我们将介绍如何解决它:)

public void timer_Tick(object sender, object e)
{
if (sw.Elapsed < duration)
{
    Seconds = (int)(duration - sw.Elapsed).TotalSeconds;
    TimeElapsed = String.Format("{0} second(s)", Seconds);
}
else
{
    TimeElapsed = "Times Up";
    timer.Stop();
    new Views.EquationView().OnTimesUp();
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#中出错:“非静态字段,方法或属性需要对象引用” - Error in C#: “An object reference is required for the non-static field, method, or property” C#错误:非静态字段,方法或属性需要对象引用 - C# Error: Object reference is required for the non-static field, method, or property 为什么我在C#中得到“非静态字段,方法或属性需要对象引用”错误? - Why am I getting “object reference is required for the non-static field, method, or property” error in C#? C# 错误:“非静态字段、方法或属性需要对象引用” - C# error: "An object reference is required for the non-static field, method, or property" C#错误(使用接口方法):非静态字段,方法或属性需要对象引用 - C# error(using interface methods): An object reference is required for the non-static field, method, or property C#错误:非静态字段,方法或属性需要对象引用 - C# error: An object reference is required for the non-static field, method, or property 非静态字段,方法或属性错误需要对象引用 - An object reference is required for the non-static field, method or property error 错误:非静态字段,方法或属性需要对象引用 - Error: An object reference is required for the non-static field, method, or property 对象引用是必需的非静态字段,方法或属性错误 - An object reference is required non-static field, method, or property error C#错误:非静态字段需要对象引用 - C# error : An object reference is required for the non-static field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM