【问题标题】:WPF Prism application does not ShutdownWPF Prism 应用程序不关闭
【发布时间】:2012-01-12 00:43:10
【问题描述】:

我编写了最简单的 WPF Prism 应用程序。它不会关闭。

App.xaml

<Application x:Class="PrismApp.Desktop.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="Shell.xaml"
             ShutdownMode="OnMainWindowClose">
    <Application.Resources>

    </Application.Resources>
</Application>

App.xaml.cs

namespace PrismApp.Desktop
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            Bootstrapper bootstrapper = new Bootstrapper();
            bootstrapper.Run();
        }
    }
}

Bootstrapper.cs

namespace PrismApp.Desktop
{
    class Bootstrapper : UnityBootstrapper
    {
        protected override System.Windows.DependencyObject CreateShell()
        {
            var shell = new Shell();
            return shell;
        }

        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();
        }
    }
}

Shell.xaml

<Window x:Class="PrismApp.Desktop.Shell"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Shell" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>

为什么会这样?

【问题讨论】:

  • 只需从 App.xaml 中删除 StartupUri="Shell.xaml" 看看发生了什么?您仍然不允许引导程序加载您的 Shell,并且 StartupUri 已被接管。
  • 启动 Uri 是你的问题。查看此 [post][1] [1]:stackoverflow.com/questions/4114956/…

标签: wpf prism-4


【解决方案1】:

我刚刚浏览了我的 prism 项目,但设置略有不同。也许会有所帮助

在 app.xaml 中,我确实没有

StartupUri="Shell.xaml"
ShutdownMode="OnMainWindowClose"

然后在 app.xaml.cs 我有以下

private void StartupContainer()
{
   Bootstrapper bootstrapper = new Bootstrapper();
   bootstrapper.Run();
   bootstrapper.ShowDefaultView();
}

在我的 bootstrapper.cs 中

Shell _shell;

protected override DependencyObject CreateShell()
{
    _shell = Container.Resolve<Shell>();
    return _shell;
}

public void ShowDefaultView()
{
    _shell.Show();
}

Shell 是我的 WPF 窗口

【讨论】:

  • 您确实意识到您不必定义自己的Shell 字段,因为Bootstrapper 已经提供了一个Shell 属性。此外,您应该覆盖 InitializeShell,而不是 ShowDefaultView。
猜你喜欢
  • 2015-08-08
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
相关资源
最近更新 更多