繁体   English   中英

如何在现有的SplashScreen中使用外部LoadingIcon项目? (WPF)

[英]How can I use an external LoadingIcon project in an existing SplashScreen? (WPF)

我下载了一个带有很好的LoadingIcon的项目( https://elegantcode.com/2009/08/21/a-simple-wpf-loading-animation/ )。 我在主项目中引用了它,但不确定如何将其实现到我的应用程序中。

我将xmlns:control="clr-namespace:LoadingControl.Control"放入主Splash.xaml中,然后尝试通过<control:LoadingAnimation HorizontalAlignment="Center" VerticalAlignment="Center"/>对其进行调用。 我也尝试过复制LoadingAnimation.xaml的整个XML代码,但这也不起作用。

启动屏幕的想法是在应用程序初始化时显示一些动画。

但是:仅当初始化不在负责呈现UI的UI-Thread上运行时,这才起作用。 因为如果是这样,将根本没有时间更新UI。

尝试在新的/不同的线程上运行初始化代码(Task.Factory.StartNew())

请记住,很多WPF Things必须在UI线程上运行,因此在这种情况下,您可能需要调用Dispatcher.Invoke()。

在主窗口的构造函数中使用如下代码:

public MainWindow()
{
    InitializeComponent();

    var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
    Task.Factory.StartNew(delegate { }).ContinueWith(async delegate
    {
        Window win = new Window() {
            WindowStyle =  WindowStyle.None, Topmost = true, ResizeMode = ResizeMode.NoResize, ShowInTaskbar = false, SizeToContent=  SizeToContent.WidthAndHeight,
            WindowStartupLocation = WindowStartupLocation.CenterOwner, Owner = this
        };

        win.Content = new LoadingAnimation();

        win.Show();
        await Task.Delay(TimeSpan.FromSeconds(4));
        win.Close();
    }, scheduler);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM