0

我正在尝试使用控件内部的 Caliburn Micro 为 Windows Phone 应用程序构建用户控件。引导程序通常设置在 App 资源中

  <Application.Resources>
     <local:AppBootstrapper x:Key="bootstrapper" />
  </Application.Resources>

我尝试在用户控件的 Xaml 中执行此操作。

 <UserControl.Resources>
     <local:AppBootstrapper x:Key="bootstrapper" />
 </UserControl.Resources>

但这会在加载组件时在初始化期间引发异常。LoadComponent 调用引发异常:“Microsoft.Phone.ni.dll 中出现‘System.InvalidOperationException’类型的第一次机会异常”

应该在何时何地初始化引导程序?

4

2 回答 2

0

由于您在 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,但据我所知,这应该做原始引导程序所做的事情,但跳过了应用程序和根框架的创建。您只需要提供应用程序和根框架(或者如果您可以从应用程序对象获取根框架,则可能只是应用程序 - 就像我说的那样,不知道有什么可能)

于 2012-12-20T09:43:21.080 回答
0

它不打算在 UserControl 的资源中使用,所以我不能保证任何好的或坏的行为。引导程序应该在应用程序资源中使用,或者您可以直接在代码中实例化它。尝试在 InitializeComponent 调用之后在用户控件的 ctor 中创建它。

于 2012-12-19T17:59:04.553 回答