简体   繁体   English

非静态字段,方法或属性需要对象引用

[英]object reference is required for the non-static field, method, or property

hmm I seem to have a problem, on my main window I am trying to do this: 嗯,我似乎有问题,在我的主窗口上,我试图这样做:

    public static readonly DependencyProperty StudentIDProperty = DependencyProperty.Register("StudentID", typeof(String), typeof(LoginWindow), new PropertyMetadata(OnStudentIDChanged));

    public string StudentID
    {
        get { return (string)GetValue(StudentIDProperty); }
        set { SetValue(StudentIDProperty, value); }
    }

    static void OnStudentIDChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        (d as LoginWindow).OnStudentIDChanged(e); // 
    }

On my other window I have this: 在我的另一个窗口上,我有:

MainWindow.StudentID = (String)((Button)sender).Tag;

But I get the error: 但是我得到了错误:

An object reference is required for the non-static field, method, or property 'WpfApplication4.MainWindow.StudentID.get'

Does anyone know how I can fix this? 有谁知道我该如何解决? It works for my user controls but not other windows? 它适用于我的用户控件,但不适用于其他窗口吗?

My main window is actually named MainWindow so I may have had this confused. 我的主窗口实际上命名为MainWindow,所以我可能对此感到困惑。

You need to set StudentID on an instance of your MainWindow class. 您需要在MainWindow类的实例上设置StudentID。 Try 尝试

((MainWindow)Application.Current.MainWindow).StudentID = (String)((Button)sender).Tag;

Because MainWindow is the name of your class, not an instance of MainWindow. 因为MainWindow是类的名称,所以不是MainWindow的实例。 You need something like: 您需要类似:

MainWindow mw = new MainWindow();
mw.StudentID = (String)((Button)sender).Tag;

I was trying to update a TextBox in the MainWindow from a UserControl and get the error 我试图从UserControl更新MainWindowTextBox并得到错误

Error 1: An object reference is required for the non-static field, method, or property 

'WpfApplication1.MainWindow.textBox1'

I resolved this error by writing the following: 我通过编写以下代码解决了该错误:

//MainWindow.textBox1.Text = ""; //Error 1

((MainWindow)Application.Current.MainWindow).textBox1.Text = "";//This is OK!

This was suggested by Rytis I 这是由Rytis I建议的

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

相关问题 非静态字段,方法或属性需要对象引用 - 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 非静态字段,方法或属性是否需要对象引用? - 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 非静态字段,方法或属性(数据集)需要对象引用 - An object reference is required for the non-static field, method, or property (dataset) 错误:非静态字段,方法或属性需要对象引用 - 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 非静态字段,方法或属性需要对象引用 - Object reference is required for non-static field, method or property 非静态字段、方法或属性需要 object 引用 - An object reference is required for the non-static field, method, or property GetAbbreviatedMonthName中的非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property in GetAbbreviatedMonthName
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM