2016-01-13 77 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访问客户端。