我想初始化MobileServices客户端的一个实例。我想在共享的单例对象上创建一个实例,以便我的应用程序中的所有页面都可以使用它。我尝试在App.xaml.cs中的Application的构造函数上初始化它,但遇到抛出的令人讨厌的本机异常。什么是初始化MobileServices客户端的最佳位置?初始化MobileServicesClient的最佳位置
- 您的主页的构造函数?
- 您的主页的加载事件(做空检查后)?
- 启动应用程序的事件处理程序?
我想初始化MobileServices客户端的一个实例。我想在共享的单例对象上创建一个实例,以便我的应用程序中的所有页面都可以使用它。我尝试在App.xaml.cs中的Application的构造函数上初始化它,但遇到抛出的令人讨厌的本机异常。什么是初始化MobileServices客户端的最佳位置?初始化MobileServicesClient的最佳位置
移动服务快速入门将其内联初始化为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
访问客户端。