【问题标题】:How can I use an external LoadingIcon project in an existing SplashScreen? (WPF)如何在现有的 SplashScreen 中使用外部 LoadingIcon 项目? (WPF)
【发布时间】:2017-02-14 08:59:58
【问题描述】:

我下载了一个带有漂亮 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 代码,但也没有用。

【问题讨论】:

  • 请发布代码以重现问题并说明它是如何不工作的。

标签: c# wpf xaml splash-screen


【解决方案1】:

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

但是:这仅在初始化在负责呈现 UI 的 UI 线程上运行时才有效。因为如果是这样,用户界面就没有时间更新了。

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

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

【讨论】:

    【解决方案2】:

    在主窗口的构造函数中使用类似的东西:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      相关资源
      最近更新 更多