由于您在 UserControl 上放置了引导程序,因此似乎PhoneApplicationService
可能已经实例化 - 您是否尝试将引导程序放在应用程序资源部分?
CM 源代码显示 CMPhoneApplicationService
在引导程序初始化时创建了一个新实例,这看起来像问题,您的应用程序的根必须已经创建了一个实例。
您是否有理由不能以标准方式(在应用程序资源中)使用 boostrapper?您的 App.xaml 是否包含初始化程序PhoneApplicationService
?
编辑:
您收到错误的 CM 源位于PrepareApplication
. 例如
protected override void PrepareApplication() {
base.PrepareApplication();
phoneService = new PhoneApplicationService();
phoneService.Activated += OnActivate;
phoneService.Deactivated += OnDeactivate;
phoneService.Launching += OnLaunch;
phoneService.Closing += OnClose;
Application.ApplicationLifetimeObjects.Add(phoneService);
if (phoneApplicationInitialized) {
return;
}
RootFrame = CreatePhoneApplicationFrame();
RootFrame.Navigated += OnNavigated;
phoneApplicationInitialized = true;
}
您可能只是将其子类化,而不是创建新的 PhoneApplicationService 而是重用现有的:
/// <summary>
/// A custom bootstrapper designed to setup phone applications.
/// </summary>
public class CustomPhoneBootstrapper : Bootstrapper {
bool phoneApplicationInitialized;
PhoneApplicationService phoneService;
/// <summary>
/// The root frame used for navigation.
/// </summary>
public PhoneApplicationFrame RootFrame { get; private set; }
/// <summary>
/// Provides an opportunity to hook into the application object.
/// </summary>
protected override void PrepareApplication(PhoneApplicationService phoneAppService, PhoneApplicationFrame rootFrame) {
base.PrepareApplication();
phoneService = phoneAppService;
phoneService.Activated += OnActivate;
phoneService.Deactivated += OnDeactivate;
phoneService.Launching += OnLaunch;
phoneService.Closing += OnClose;
Application.ApplicationLifetimeObjects.Add(phoneService);
if (phoneApplicationInitialized) {
return;
}
RootFrame = rootFrame;
RootFrame.Navigated += OnNavigated;
phoneApplicationInitialized = true;
}
void OnNavigated(object sender, NavigationEventArgs e) {
if (Application.RootVisual != RootFrame) {
Application.RootVisual = RootFrame;
}
}
/// <summary>
/// Occurs when a fresh instance of the application is launching.
/// </summary>
protected virtual void OnLaunch(object sender, LaunchingEventArgs e) { }
/// <summary>
/// Occurs when a previously tombstoned or paused application is resurrected/resumed.
/// </summary>
protected virtual void OnActivate(object sender, ActivatedEventArgs e) { }
/// <summary>
/// Occurs when the application is being tombstoned or paused.
/// </summary>
protected virtual void OnDeactivate(object sender, DeactivatedEventArgs e) { }
/// <summary>
/// Occurs when the application is closing.
/// </summary>
protected virtual void OnClose(object sender, ClosingEventArgs e) { }
}
免责声明:不确定这是否会起作用,因为我从未开发过 Windows Phone,但据我所知,这应该做原始引导程序所做的事情,但跳过了应用程序和根框架的创建。您只需要提供应用程序和根框架(或者如果您可以从应用程序对象获取根框架,则可能只是应用程序 - 就像我说的那样,不知道有什么可能)