簡體   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