简体   繁体   English

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

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

I downloaded a project with a nice LoadingIcon ( https://elegantcode.com/2009/08/21/a-simple-wpf-loading-animation/ ). 我下载了一个带有很好的LoadingIcon的项目( https://elegantcode.com/2009/08/21/a-simple-wpf-loading-animation/ )。 I referenced it into my main project but I'm not sure how I can implement this into my application. 我在主项目中引用了它,但不确定如何将其实现到我的应用程序中。

I put xmlns:control="clr-namespace:LoadingControl.Control" into the main Splash.xaml and then try to call it via <control:LoadingAnimation HorizontalAlignment="Center" VerticalAlignment="Center"/> That did not work for me. 我将xmlns:control="clr-namespace:LoadingControl.Control"放入主Splash.xaml中,然后尝试通过<control:LoadingAnimation HorizontalAlignment="Center" VerticalAlignment="Center"/>对其进行调用。 I've also tried copying the whole XML code of the LoadingAnimation.xaml but that didn't work either. 我也尝试过复制LoadingAnimation.xaml的整个XML代码,但这也不起作用。

The idea of a splash Screen is to Show some Animation while the application is initializing. 启动屏幕的想法是在应用程序初始化时显示一些动画。

But: this will only work if the initializing is not running on the UI-Thread that is responsible for rendering the UI. 但是:仅当初始化不在负责呈现UI的UI-Thread上运行时,这才起作用。 Because if so, there would simply be no time for the UI to update. 因为如果是这样,将根本没有时间更新UI。

Try to run your initializing code on a new/different thread (Task.Factory.StartNew()) 尝试在新的/不同的线程上运行初始化代码(Task.Factory.StartNew())

Keep in mind that a lot of WPF Things must run on the UI thread so you might Need to call Dispatcher.Invoke() in this cases. 请记住,很多WPF Things必须在UI线程上运行,因此在这种情况下,您可能需要调用Dispatcher.Invoke()。

Use something like this in your main window's constructor: 在主窗口的构造函数中使用如下代码:

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