【问题标题】:Close with timeout is not working in Console to close splashscreen超时关闭在控制台中无法关闭启动画面
【发布时间】:2019-11-08 02:51:44
【问题描述】:

我做了一个类,我做了一个实例。在上述实例中,我有这些代码行来显示和关闭启动画面。

// Open (show)
public void ShowSplashScreen(bool autoClose = false)
{
    splashscreen.Show(autoClose, true);
}

// Close (don't show)
public void CloseSplashScreen()
{
    splashscreen.Close(TimeSpan.FromSeconds(0.3));
}

它显示得很好,但从不关闭,只是停留在那里。

这是启动画面关闭的文档:https://docs.microsoft.com/en-us/dotnet/api/system.windows.splashscreen.close?view=netframework-4.8

[System.Security.SecurityCritical]

public void Close(TimeSpan fadeoutDuration);

注意:我正在使用show 方法,参数AutoClose 设置为falseTopMost 设置为true,这使它不会自动关闭,因为我想以编程方式关闭它而不是订阅现有事件。


我正在运行控制台(.NET 框架)应用程序中的代码行以进行测试,然后再将其完全实现到我的 UI 中。


我尝试过的: 在调用close之前调试甚至尝试再次调用show


肯定是类出了问题,因为调用类并直接操作属性有效:

ClassSplashScreen rss = new ClassSplashScreen();
rss.splashscreen.Show(false);
rss.splashscreen.Close(TimeSpan.FromSeconds(1));

我最好的猜测是挂起 UI 并冻结它?但我不确定该怎么办。


运行代码来测试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace NamespaceName
{
    public class StackOverFlowCode
    {
        static void Main(string[] args)
        {
            ClassSplashScreen screen = new ClassSplashScreen();
            screen.ShowSplashScreen();
            screen.CloseSplashScreen();
        }

    }
    public class ClassSplashScreen
    {
        public SplashScreen splashscreen { get; set; }
        public ClassSplashScreen()
        {
            splashscreen = new SplashScreen("Resource Image Link");
        }
        public void ChangeSplashResource(SplashScreen resource)
        {
            splashscreen = resource;
        }
        public void ShowSplashScreen(bool autoClose = false)
        {
            splashscreen.Show(autoClose, true);
        }
        public void CloseSplashScreen()
        {
            splashscreen.Close(TimeSpan.FromSeconds(1));
        }
    }
}

【问题讨论】:

  • 它曾经调用过“CloseSplashScreen”方法吗?
  • @Luc 是的,我已经调试并确保它被调用并无异常通过。
  • 控制台?提问时请提供MCVE您的问题。
  • @mm8 我提供的代码应该就是这样,除了调用控制台应用程序(.net 框架)中的方法之外,我所做的就是这样:ShowSplashScreen() CloseSplashScreen()

标签: c# wpf methods console-application instance


【解决方案1】:

SplashScreen 依赖于调度程序,但默认情况下控制台应用程序中没有调度程序。如果您创建一个System.Windows.Application,它应该可以按预期工作:

public class StackOverFlowCode
{
    [STAThread]
    static void Main(string[] args)
    {
        Application app = new Application();
        app.Startup += (s, e) => 
        {
            ClassSplashScreen screen = new ClassSplashScreen();
            screen.ShowSplashScreen();
            screen.CloseSplashScreen();
        };
        app.Run();
    }
}

public class ClassSplashScreen
{
    private readonly SplashScreen splashscreen;

    public ClassSplashScreen() => splashscreen = new SplashScreen("Resource Image Link");

    public void ShowSplashScreen() => splashscreen.Show(false);

    public void CloseSplashScreen() => splashscreen.Close(TimeSpan.FromSeconds(1));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多