UWP 中 App.xaml.cs 与 MainPage.xaml.cs 共享变量

文章目录

    在 App.xaml.cs 定义一个 static 变量,然后初始化一个值

    sealed partial class App : Application
        {
            public static int counter { get; set; }
    
            public App()
            {
                this.InitializeComponent();
                this.Suspending += OnSuspending;
                App.counter = 10;       // 初始化
            }
    

    在 MainPage.xaml.cs 中读取变量 counter

    public MainPage()
    {
        this.InitializeComponent();
        Debug.WriteLine(App.counter);      // 读取
    }
    

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式