Skip to main content
 首页 » 编程设计

c#之如何让我的 Windows Phone 7 应用程序中的某些事情只发生一次

2024年02月23日17luoye11

我只需要做一次(在我的例子中创建一个类的实例)。我能找到的最接近的东西是将它放在 PhoneApplicationPage_Loaded 事件处理程序中,或者放在 MainPage.xaml 的顶部。但这对我不起作用,因为我的应用程序中有其他页面,所以如果我从另一个页面导航回 MainPage,它会再次执行该代码。

谢谢,大卫

请您参考如下方法:

当您创建一个新项目时,您会发现一个 App.xaml.cs 文件已添加到您的项目中。您可以在此处添加在应用程序生命周期的不同时间点执行的代码。您可以将代码添加到处理 Launching 事件的方法中:

// Code to execute when the application is launching (eg, from Start) 
// This code will not execute when the application is reactivated 
private void Application_Launching(object sender, LaunchingEventArgs e) 
{ 
   // your code goes here 
} 

这段代码只会在您的应用程序启动时执行一次。