如何在 WPF 启动初始屏幕上显示 dll 的加载?



我正在寻找一个应用程序启动初始屏幕。

在该屏幕上,我想在加载应用程序 dll 时显示它们(就像运行项目时在 Visual Studio 的"输出"窗口中看到的那样(

有人知道我该怎么做吗?非常感谢。

我通过将主应用程序创建为"启动画面"来做到这一点。 我在下面的实例中只有 1 个 DLL,但你可以有几个。 在主窗口中,我使用了一个 DockPanel,而不是网格,进度条停靠在底部,IsIndeterminate="True",一个名为 status 的文本块也停靠在底部(由于某种原因我无法绑定工作(,以及一个图像(未停靠,填充窗口的上部(......

    public MainWindow()
    {
        InitializeComponent();
        // Wait for screen to be shown before continuing...
        ContentRendered += AppLoader;
    }
    private void AppLoader(object sender, EventArgs e)
    {
        // we want to make sure this only happens once...
        ContentRendered -= AppLoader;
        // use a background worker...
        BackgroundWorker bgw = new BackgroundWorker();
        // allows us to update the status...
        bgw.WorkerReportsProgress = true;
        bgw.ProgressChanged += Bgw_ProgressChanged;
        // here is where we hide the main window and continue with the application...
        bgw.RunWorkerCompleted += Bgw_RunWorkerCompleted;
        // load dll's, check security, etc.
        bgw.DoWork += Bgw_DoWork;
        bgw.RunWorkerAsync();
    }
    [STAThread]
    private void Bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Result != null)
        {
            // show main window and hide this "Splash Screen" window
            Window window = (Window)Activator.CreateInstance((Type)e.Result);
            Hide();
            window.ShowDialog();
        }
        else
            MessageBox.Show(this, "Unable to find local resources.", Title, MessageBoxButton.OK, MessageBoxImage.Error);
        Application.Current.Shutdown();
    }
    public void UpdateUI()
    {            
        DispatcherFrame frame = new DispatcherFrame();
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, new DispatcherOperationCallback(delegate (object parameter)
        {
            frame.Continue = false;
            return null;
        }), null);
        Dispatcher.PushFrame(frame);
    }
    private void Bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Status.Text = $"{e.UserState}";
        UpdateUI();
    }
    private void Bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker Bgw = (BackgroundWorker)sender;
        e.Result = null;
        ConfigFile Cfg = new ConfigFile();
        if (Cfg.Count==0)
        {
            MessageBox.Show(this, "ERROR:  XmlCfg missing or not accessable.", Title, MessageBoxButton.OK, MessageBoxImage.Error);
            Application.Current.Shutdown();
        }
        if (AppDLL.IsAdmin)
        {
            // load dll...
            Bgw.ReportProgress(0, "Loading...");
            e.Result = AppDLL.GetWindow("./TAC.dll", "AppWindow");
        }
        else
        {
            MessageBox.Show(this, "ERROR:  Admin credentials required.", Title, MessageBoxButton.OK, MessageBoxImage.Error);
            Application.Current.Shutdown();
        }
    }

你可以尝试如下:

AppDomain currentDomain = AppDomain.CurrentDomain;
      currentDomain.AssemblyLoad += new AssemblyLoadEventHandler(MyAssemblyLoadEventHandler);
static void MyAssemblyLoadEventHandler(object sender, AssemblyLoadEventArgs args) {
      var assemblyName = args.LoadedAssembly.FullName;
      /* use this name to pass to your splash screen */
   }

最新更新