简体   繁体   中英

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/ ). 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. I've also tried copying the whole XML code of the LoadingAnimation.xaml but that didn't work either.

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. Because if so, there would simply be no time for the UI to update.

Try to run your initializing code on a new/different thread (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.

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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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