WPF-从MainWindow.xaml.cs类,为什么我无法访问App.xam.cs类的自定义属性

问题描述 投票:0回答:1

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'
    }
..........
..........
}
c# wpf xaml
1个回答
0
投票

您需要将Name属性设为静态,因为它看起来根本就没有在MainWindow类中实例化App类。

......
......
public partial class App : Application
{
    private static string[] name;
    public static string[] Name
    {
        get { return name; }
        set { name = value; }
    }
......
......
}
© www.soinside.com 2019 - 2024. All rights reserved.