如何在WPF启动启动画面上显示dll的加载?

时间:2018-02-01 14:51:47

标签: c# wpf

我正在寻找创建应用程序启动启动画面。

在该屏幕上,我想在加载时显示应用程序dll (就像在运行项目时在visual studio的“输出”窗口中看到的那样)

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

2 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

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 */

   }

答案 1 :(得分:0)

我是通过将主应用程序创建为" Splash"屏幕。我在下面的实例中只有1个DLL,但你可能有几个。在主窗口中,我使用DockPanel,底部停靠了一个进度条,IsIndeterminate =" True&#34 ;,一个名为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();
        }
    }