无法使 Caliburn.Micro 在 Windows Phone 上运行

发布于 2024-12-16 10:52:59 字数 3628 浏览 2 评论 0原文

我试图了解 Caliburn.Micro 如何与 Windows Phone(以及一般的 MVVM)配合使用,所以我创建了一个基本 Windows Phone 应用程序,已安装 Caliburn.Micro NuGet 包(v1.2.0 - 最新版本现在)和按照这里那里的一些说明进行操作。所以,我最终得到:

WMAppManifest.xml

<DefaultTask  Name ="_default" NavigationPage="Views/HomeView.xaml"/>

Framework/AppBootstrapper.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using Caliburn.Micro;
using MyCaliburn.PhoneUI.ViewModels;

namespace MyCaliburn.PhoneUI.Framework
{
    public class AppBootstrapper : PhoneBootstrapper
    {
        PhoneContainer container;

        protected override void Configure()
        {
            container = new PhoneContainer(RootFrame);
            container.RegisterPhoneServices();
            container.Singleton<HomeViewModel>();
        }

        protected override void OnUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (Debugger.IsAttached)
            {
                Debugger.Break();
                e.Handled = true;
            }
            else
            {
                MessageBox.Show("An unexpected error occured, sorry about the troubles.", "Oops...", MessageBoxButton.OK);
                e.Handled = true;
            }

            base.OnUnhandledException(sender, e);
        }

        protected override object GetInstance(Type service, string key)
        {
            return container.GetInstance(service, key);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            container.BuildUp(instance);
        }
    }
}

ViewModels/HomeViewModel.cs

using Caliburn.Micro;

namespace MyCaliburn.PhoneUI.ViewModels
{
    public class HomeViewModel : Screen
    {
        public HomeViewModel()
        {
            //DisplayName = "Home";
        }
    }
}

View/HomeView.xaml。 cs (XAML 页面是默认的 Window Phone 纵向页面)

using Microsoft.Phone.Controls;

namespace MyCaliburn.PhoneUI.Views
{
    public partial class HomeView : PhoneApplicationPage
    {
        public HomeView()
        {
            InitializeComponent();
        }
    }
}

App.xaml

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="MyCaliburn.PhoneUI.App"
    xmlns:Framework="clr-namespace:MyCaliburn.PhoneUI.Framework">

    <!--Application Resources-->
    <Application.Resources>
        <Framework:AppBootstrapper x:Key="bootstrapper" />
    </Application.Resources>

</Application>

App.xaml.cs

using System.Windows;

namespace MyCaliburn.PhoneUI
{
    public partial class App : Application
    {
        /// <summary>
        /// Constructor for the Application object.
        /// </summary>
        public App()
        {
            // Standard Silverlight initialization
            InitializeComponent();
        }
    }
}

现在,当我点击F5,应用程序运行并退出,不会显示任何页面或异常,并且不会遇到我设置的任何断点。

谁能告诉我代码中缺少什么导致应用程序无法运行?

提前致谢。

I'm trying to understand how Caliburn.Micro works with Windows Phone (and MVVM in general) so I created a basic Windows Phone Application, installed Caliburn.Micro NuGet package (v1.2.0 - the latest for now) and followed the few instructions here and there. So, I ended up with:

WMAppManifest.xml

<DefaultTask  Name ="_default" NavigationPage="Views/HomeView.xaml"/>

Framework/AppBootstrapper.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using Caliburn.Micro;
using MyCaliburn.PhoneUI.ViewModels;

namespace MyCaliburn.PhoneUI.Framework
{
    public class AppBootstrapper : PhoneBootstrapper
    {
        PhoneContainer container;

        protected override void Configure()
        {
            container = new PhoneContainer(RootFrame);
            container.RegisterPhoneServices();
            container.Singleton<HomeViewModel>();
        }

        protected override void OnUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (Debugger.IsAttached)
            {
                Debugger.Break();
                e.Handled = true;
            }
            else
            {
                MessageBox.Show("An unexpected error occured, sorry about the troubles.", "Oops...", MessageBoxButton.OK);
                e.Handled = true;
            }

            base.OnUnhandledException(sender, e);
        }

        protected override object GetInstance(Type service, string key)
        {
            return container.GetInstance(service, key);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            container.BuildUp(instance);
        }
    }
}

ViewModels/HomeViewModel.cs

using Caliburn.Micro;

namespace MyCaliburn.PhoneUI.ViewModels
{
    public class HomeViewModel : Screen
    {
        public HomeViewModel()
        {
            //DisplayName = "Home";
        }
    }
}

View/HomeView.xaml.cs (the XAML page is the default Window Phone Portrait Page)

using Microsoft.Phone.Controls;

namespace MyCaliburn.PhoneUI.Views
{
    public partial class HomeView : PhoneApplicationPage
    {
        public HomeView()
        {
            InitializeComponent();
        }
    }
}

App.xaml

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="MyCaliburn.PhoneUI.App"
    xmlns:Framework="clr-namespace:MyCaliburn.PhoneUI.Framework">

    <!--Application Resources-->
    <Application.Resources>
        <Framework:AppBootstrapper x:Key="bootstrapper" />
    </Application.Resources>

</Application>

App.xaml.cs

using System.Windows;

namespace MyCaliburn.PhoneUI
{
    public partial class App : Application
    {
        /// <summary>
        /// Constructor for the Application object.
        /// </summary>
        public App()
        {
            // Standard Silverlight initialization
            InitializeComponent();
        }
    }
}

Now, when I hit F5, the application runs and exits without showing any page or exception and doesn't hit any breakpoints that I sit.

Can anyone tells me what's missing in my code which prevents the application from running?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半暖夏伤 2024-12-23 10:52:59

很多时候,当我最终得到一个无法启动的应用程序时 - 事实证明,由于一些重构,App 类不再是启动对象。右键单击解决方案资源管理器中的项目,转到属性/应用程序并确保启动对象设置正确。

Many times when I end up with an app that does not start - it turns out that due to some refactoring the App class is not the startup object any more. Right-click on the project in solution explorer, go to properties/Application and make sure Startup object is set correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文