【问题标题】:Closing of a WPF Window still visible in taskbar关闭 WPF 窗口在任务栏中仍然可见
【发布时间】:2016-03-09 09:52:45
【问题描述】:

我的 WPF 应用程序有一个小问题。

我有一个初始图像(作为 XAML 窗口)和主应用程序(作为另一个从 Splash 调用的 XAML 窗口)

即使我在启动窗口上使用此.Close() 并在主应用程序上工作,启动窗口仍然在任务栏中可见。

如果我要在主应用程序上使用这个.Close,我将在任务栏应用程序中有两个空白窗口,我必须按 X 才能完全关闭。

我也尝试过Application.Current.Shutdown(),但结果相同。

飞溅:

    public Splash()
    {
        InitializeComponent();
    }

    private void Window_ContentRendered(object sender, EventArgs e)
    {
        CloseProcedure(); 
    }

    public async void CloseProcedure()
    {
        //Missing data loading which will be async.            
        UC_Main UCMain = new UC_Main();
        MainWindow window = new MainWindow();
        window.AppContent.Children.Add(UCMain);
        this.Close();
        await Task.Delay(500); //for fade effect.
        window.Show();
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        Closing -= Window_Closing;
        e.Cancel = true;
        var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(0.5));
        this.BeginAnimation(UIElement.OpacityProperty, anim);
        anim.Completed += (s, _) => this.Close();
    }

主应用

        private void BTN_Close_MouseUp(object sender, MouseButtonEventArgs e)
    {
        this.Close();
    }

    private void titleBar_MouseDown(object sender, MouseButtonEventArgs e)
    {
        titleBar.Background = Brushes.White;
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        Closing -= Window_Closing;
        e.Cancel = true;
        var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(0.2));
        this.BeginAnimation(UIElement.OpacityProperty, anim);
        anim.Completed += (s, _) => Application.Current.Shutdown();
    }
}

【问题讨论】:

标签: c# wpf xaml


【解决方案1】:

WPF 应用程序的默认关闭模式是“OnLastWindowClose”,这意味着一旦关闭最后一个窗口,就会调用应用程序关闭过程,因此您不需要显式编写 System.Windows.Application.Shutdown( )。

您可能有一些引用未关闭的启动屏幕的内容。当你打开你的主窗口时,你应该在那个时候正确地关闭启动画面,并确保对它的任何引用都是空的。

如果您发布所有代码,将更容易知道正确的修复方法。

首先要防止窗口显示在任务栏中,请设置 Window XAML 声明的 ShowInTaskbar 属性:

<Window ShowInTaskbar="False" ...

【讨论】:

  • 添加了代码。尝试 ShowInTaskbar="False" 解决了启动窗口可见的问题,但问题并未解决,因为关闭主窗口仍会在任务栏中留下空白应用程序并运行进程。
  • 一开始你是怎么打开splash的?从哪个地方,通过什么方法?
  • 是app.xaml中定义的默认启动页面。
【解决方案2】:

我已经想出了一个解决方案。 我没有尝试关闭进程并在关闭事件处理程序中设置淡入淡出动画,而是编写了一个异步方法,该方法将淡出屏幕,等待相同的时间然后终止进程。像魅力一样工作。

    private void BTN_Close_MouseUp(object sender, MouseButtonEventArgs e)
    {
        this.FadeOut();
    }

    async private void FadeOut()
    {
        var anim = new DoubleAnimation(0, (Duration)TimeSpan.FromSeconds(0.2));
        this.BeginAnimation(UIElement.OpacityProperty, anim);
        await Task.Delay(200);
        this.Close(); 
        System.Diagnostics.Process.GetCurrentProcess().Kill();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    相关资源
    最近更新 更多