2016-01-13 45 views
1

我想初始化MobileServices客戶端的一個實例。我想在共享的單例對象上創建一個實例,以便我的應用程序中的所有頁面都可以使用它。我嘗試在App.xaml.cs中的Application的構造函數上初始化它,但遇到拋出的令人討厭的本機異常。什麼是初始化MobileServices客戶端的最佳位置?初始化MobileServicesClient的最佳位置

  • 您的主頁的構造函數?
  • 您的主頁的加載事件(做空檢查後)?
  • 啓動應用程序的事件處理程序?

回答

1

做到這一點,最好的地方是在App.Xaml.cs一個靜態變量,就像這樣:

namespace myapp 
{ 
    /// <summary> 
    /// Provides application-specific behavior to supplement the default Application class. 
    /// </summary> 
    sealed partial class App : Application 
    { 

     // This MobileServiceClient has been configured to communicate with the Azure Mobile App. 
     // You're all set to start working with your Mobile App! 
     public static MobileServiceClient MobileService = new MobileServiceClient("https://my-apservice.azurewebsites.net"); 

您可以再與App.MobileService訪問客戶端。