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

时间:2021-09-09 22:47:09

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.

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

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.

我把xmlns:control =“clr-namespace:LoadingControl.Control”放到主Splash.xaml然后尝试通过 调用它对我来说不起作用。我也尝试复制LoadingAnimation.xaml的整个XML代码,但这也不起作用。

2 个解决方案

#1


0  

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.

但是:只有在负责呈现UI的UI-Thread上没有运行初始化时,这才有效。因为如果是这样,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事物必须在UI线程上运行,因此在这种情况下您可能需要调用Dispatcher.Invoke()。

#2


0  

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

}

#1


0  

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.

但是:只有在负责呈现UI的UI-Thread上没有运行初始化时,这才有效。因为如果是这样,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事物必须在UI线程上运行,因此在这种情况下您可能需要调用Dispatcher.Invoke()。

#2


0  

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

}