初始化移动服务客户端的最佳位置

本文关键字:最佳位置 客户端 服务 移动 初始化 | 更新日期: 2023-09-27 18:30:27

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

  • 您的主页的构造函数?
  • 主页的加载事件(执行空检查后)?
  • 启动了应用程序的事件处理程序?

初始化移动服务客户端的最佳位置

移动服务快速入门将其内联初始化为 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访问它。

执行此操作

的最佳位置是作为 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 访问客户端。