CompositeWPF - Displaying Loading... message while form loads

Below we'll show how to display a loading message while we wait for our form to load - we'll display the form almost immediately.

The QuickStarts \ Commanding \ App.xaml.cs has the following code:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        CommandingBootstrapper bootstrapper = new CommandingBootstrapper();
        bootstrapper.Run();
    }
}

Which has the bootstrapper loading the Shell and showing it

class CommandingBootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        Shell shell = Container.Resolve<Shell>();
        shell.Show();
        return shell;
    }
    protected override IModuleEnumerator GetModuleEnumerator()
    {
        return new StaticModuleEnumerator().AddModule(typeof(OrderModule));
    }
}

Since the SDMS solution has the bootstrapper in the CompositeWPF.Library we can't use the above code so we provide the BootStrapper a delegate for the CreateShell method that it should call.   The SDMS \ App \ CommandPrototype application has the following:

public partial class App : Application
{
    public App()
    {
        BootStrapper bootStrapper = new BootStrapper(CreateShell, GetType());
        bootStrapper.Run();
    }
    protected DependencyObject CreateShell(IUnityContainer container)
    {
        // Launch our shell
        Window1 shell = container.Resolve<Window1>();
        shell.Show();
        return shell;
    }
}

With both of the above configurations note how much time transpires from the time the Bootstrapper is started (21:22:30:102) to the time the Shell normally is displayed (21:22:31:205 - the arrow). 

We won't use the above two default methods of loading and showing the form when the BootStrapper.CreateShell() method fires; we want to show a "Loading..." message.  As you can see in the screenshot below the *first* thing we do is load the form and show it - we then start loading the application.

After the Shell is loaded (21:22:32:290) the CompositeWPF then proceeds to load and initialize modules - whether using the new process or default the form is displayed while loading continues...

The following reflects what the SDMS user will see almost immediately - until the form is completely loaded.  The loading message is in a textblock which can be replaced with pictures or progressbar (being updated at strategic intervals); for now I'm going for core functionality.

What the user sees after the form is loaded follows - we collapse the textblock so the text is no longer visible:

Click HERE for flash demo

Click HERE for demo of different app without loader modifications

The following is the new code in the App.xaml.cs file:

namespace MainShell
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private Shell shell;         /// <summary>
        /// Loads the application.
        /// Bootstrapper is in the Shell.Infrastructure project which
        /// can be shared by multiple solutions.   If you specify a type
        /// as a second parameter (as we do here) versus an IModuleEnumerator
        /// method, the BootStrapper will load all modules in the app
        /// directory (any module it has a reference to).
        /// </summary>
        public App()
        {
            // Display shell with Loading... message
            ShowShell();             // Configure the container and load modules
            BootStrapper bootStrapper = new BootStrapper(CreateShell, GetType());
            bootStrapper.Run();
           
            // Hide the loading message
            shell.tbLoading.Visibility = Visibility.Collapsed;
        }         protected DependencyObject CreateShell(IUnityContainer container)
        {
            container.Resolve<ILogger>()
                .Log("CreateShell (Buildup)", Category.Debug, Priority.Low);             // At this point the bootstrapper has configured the
            // unity container - we can buildup our shell
            container.BuildUp(shell);
            return shell;
        }         /// <summary>
        /// Show the shell immediately - the shell XAML has a Loading...
        /// textblock
        /// </summary>
        protected void ShowShell()
        {
            // We'll have to manually instantiate logger; this will be
            // the only place we tightly couple because we don't have
            // unity container yet.
            ILogger Logger = new TraceToolLogger();
            Logger.Log("Loading Shell", Category.Debug, Priority.Low);             // Instantiate the shell
            shell = new Shell();
            WF.Application.DoEvents(); // prevents black background
            shell.Show();
            WF.Application.DoEvents(); // allows form to completely paint             // This was called before the bootstrapper, the logger
            // won't be in scope so we'll log the entry here
            Logger.Log("Loading Bootstrapper", Category.Debug, Priority.Low);
        }
    }

Source code available HERE - Change Set: 22520

 


Tags: , ,
Categories: CompositeWPF


Actions: E-mail | Permalink |  Grammar/Typo/Better way? Please let me know