初始化MobileServicesClient的最佳位置

时间:2016-01-13 20:08:19

标签: c# uwp azure-mobile-services

我正在尝试初始化MobileServices客户端的实例。我想在共享单例对象上创建一个实例,我的应用程序中的所有页面都可以使用它。我尝试在App.xaml.cs中的Application的构造函数上初始化它,但遇到了一个令人讨厌的本机异常被抛出。初始化MobileServices客户端的最佳位置是什么?

  • 主页的构造函数?
  • 主页的加载事件(进行空检查后)?
  • 推出了应用程序的事件处理程序?

2 个答案:

答案 0 :(得分:6)

移动服务快速入门在App.xaml.cs中将其内联初始化为公共静态。例如:https://github.com/Azure/azure-mobile-apps-quickstarts/blob/master/client/windows-universal-cs/ZUMOAPPNAME/ZUMOAPPNAME.Shared/App.xaml.tt#L32

然后,您可以从应用代码中App.MobileService访问它。

答案 1 :(得分: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访问客户端。

相关问题