Question

I have a class (code included below) that I used as my Startup class. This class instanciate my regular partial class app.cs (app.xaml).

I thought that it was the compiler which instantiate my partial class and set parameters (defined in xaml). But it should be something else because my assertion defined in SingleInstanceApp.cs is false.

Why my assertion is false, why StartupUri is null ????????

Thanks, Eric

Startup class:

namespace MonitorMe
{
    public class SingleInstanceApp
    {
        [STAThread]
        public static void Main(string[] args)
        {
            Mutex _mutexSingleInstance = new Mutex(true, "MonitorMeSingleInstance");

            if (_mutexSingleInstance.WaitOne(TimeSpan.Zero, true))
            {
                try
                {
                    var app = new App();

                    // ASSERTION HERE is FALSE ... WHY
                    Debug.Assert(app.StartupUri != null);

                    app.Run(new MainWindow());
                }
                finally
                {
                    _mutexSingleInstance.ReleaseMutex();
                    _mutexSingleInstance.Close();
                }
            }
            else
            {
                MessageBox.Show("One instance is already running.");

                var processes = Process.GetProcessesByName(Assembly.GetEntryAssembly().GetName().Name);
                {
                    if (processes.Length > 1)
                    {
                        foreach (var process in processes)
                        {
                            if (process.Id != Process.GetCurrentProcess().Id)
                            {
                                WindowHelper.SetForegroundWindow(process.MainWindowHandle);
                            }
                        }
                    }
                }
            }
        }
    }
}

My regular App class xaml (.cs has nothing in it):

<Application x:Class="MonitorMe.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>
Was it helpful?

Solution

If you have ever looked at generated code (*.g.cs) located in obj folder for any WPF project, there will be a call to InitializeComponent - which loads the corrresponding XAML by calling LoadComponent.

Quoting a sample from the MSDN page Application Management Overview

// Create new instance of application subclass
App app = new App();

// Code to register events and set properties that were 
// defined in XAML in the application definition
app.InitializeComponent();

// Start running the application
app.Run();

So, you need call InitializeComponent on app object after instantiate.

I am also adding InitializeComponent code fro app.g.cs for one of my sample apps:

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {

    #line 4 "..\..\..\App.xaml"
    this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);

    #line default
    #line hidden
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top