在MainWindow.xaml.cs
类中,尝试访问Name
类中的属性App.xaml.cs
时,出现错误:'App' does not contain a definition for 'Name'
。为什么?
备注:我正在使用类似于this one的方案。但是作者那里使用的是全局变量,在大多数情况下,我们都知道using properties is preferred over global variable。因此,我正在尝试以我的情况为财产。
App.xaml.cs:
......
......
public partial class App : Application
{
private string[] name;
public string[] Name
{
get { return name; }
set { name = value; }
}
......
......
}
MainWindow.xaml.cs:
...........
...........
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string[] myArray = App.Name //VS2019 Intellisense gives the error: 'App' does not contain a definition for 'Name'
}
..........
..........
}
您需要将Name属性设为静态,因为它看起来根本就没有在MainWindow类中实例化App类。
......
......
public partial class App : Application
{
private static string[] name;
public static string[] Name
{
get { return name; }
set { name = value; }
}
......
......
}