【问题标题】:members cannot be accessed with an instance reference无法使用实例引用访问成员
【发布时间】:2015-12-25 18:46:55
【问题描述】:

为什么下面的粗体代码会出现此错误?

成员IocContainer.ShowWeatherDetailViewModel (和IocContainer.ShowWeatherViewModel 无法通过实例引用访问;改为使用类型名称对其进行限定。

错误代码:CS0176

public class IocContainer 
{
    public static IocContainer Ioc
    {
        get { return App.Current.Resources["ioc"] as IocContainer; }
    }

    // Some other static properties.
    // static constructor
}
IocContainer.Ioc.ShowWeatherDetailViewModel.Item = IocContainer.Ioc.ShowWeatherViewModel.SelectedVillage;

【问题讨论】:

  • 大概您的ShowWeatherDetailViewModel 属性也是静态的 - 所以您只需要IocContainer.ShowWeatherDetailViewModel,或者将该属性设为实例属性。
  • @JonSkeet:是的,但是我不使用App.xaml文件中资源中的IocContainer,先生。
  • 如果你想使用一个特定的实例(例如你的 app.xaml 中的实例),你应该将方法设为实例方法,而不是静态的
  • 正如 Jon 所提到的,您可能在 ShowWeatherDetailViewModel 成员的定义中有“static”关键字。如果你想访问特定实例的成员,你应该删除它。
  • 确实 - 我们基本上无法在不知道您要达到的目标的情况下为您提供帮助,并提供一个简短但完整的示例。

标签: c# ioc-container static-members instance-reference


【解决方案1】:

我通过将 IocContainer.ShowWeatherDetailViewModelIocContainer.ShowWeatherViewModel 设为实例属性找到了它。

感谢@JonSkeet 和@ChrisShain。

【讨论】:

    猜你喜欢
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多