【发布时间】: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