WPF: How to start from a window in a different assembly

13,506

Solution 1

This article gives a clean XAML-only solution.

StartupUri="pack://application:,,,/assembly_name;component/path/file_name.xaml"

Where:

  • assembly_name is the name of the referenced assembly, sans extension
  • path is the subfolder in which the component resides; if the component is at the project root, this element is omitted
  • file_name is the file name of the component

Examples:

pack://application:,,,/UI;component/CalculatorView.xaml
assembly - UI.dll
path - none (file at project root)
file_name - CalculatorView

pack://application:,,,/MyApp.UI;component/Views/CalculatorView.xaml
assembly - MyApp.UI.dll
path - Views
file_name - CalculatorView

pack://application:,,,/UI;component/Views/External/CalculatorView.xaml assembly - UI.dll
path - Views/External
file_name - CalculatorView 

Solution 2

I'd check your pack URI. Below is the uri I'd try. Think of 'component' as the root folder in your project and where I've put 'FolderName' put the appropriate folder name or remove it if Main.xaml is in the root of the project.

StartupUri = new Uri(@"pack://application:,,,/CompanyName.VisualStudio.UI;component/FolderName/Main.xaml", UriKind.Absolute);

Solution 3

Old question, but this is also helpful:

void App_Startup(object sender, StartupEventArgs e)
        {
            MainWindow = new YourWindow(some, arguments);
            MainWindow.Show();
}

and i app.xaml:

<Application
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.App"
  Startup="App_Startup" />

and rememeber about ShutdownMode : if you remember to open new window before closing last one you should be good

Share:
13,506

Related videos on Youtube

Bob
Author by

Bob

Updated on May 10, 2022

Comments

  • Bob
    Bob almost 2 years

    I googled this and still can't get it working

    I have a WPF app and want to start from Main.xaml which is located in a different assembly. Both assemblies are in the same location.

    How can I do this? I took out the StartupUri from the XAML and tried with these and some slight variations:

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
    
            StartupUri = new Uri("/CompanyName.VisualStudio.UI;CompanyName/VisualStudio/UI/DatabaseManager/Main.xaml", UriKind.Relative);
            //StartupUri = new Uri(@"pack://application:,,,/ CompanyName.VisualStudio.UI;CompanyName/VisualStudio/UI/DatabaseManager/Main.xaml");
    
        }
    

    The name of the assembly is "CompanyName.VisualStudio.UI" and the namespace is "CompanyName/VisualStudio/UI/DatabaseManager/Main.xaml"

    Any ideas?

    • Orbling
      Orbling
      I so read that as "WTF: ..." ;-)