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

更新日期: 2018-11-25 阅读次数: 6946 分类: UWP

在 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 聊聊, 查看更多联系方式