Oh My Dog
Oh My Dog

Reputation: 801

How to load a window from absolute uri?

my job is to open a WPF Window, all I have is a absolute URI:

string path = "pack://application:,,,/ExternalAssembly;component/Window1.xaml";

this window is in a different assembly, of cause.

So I try to:

Window window = Application.LoadComponent(new Uri(path, UriKind.Absolute)) as Window;

Exception raised said: can not use absolute uri. then I try to use XamlReader.Load, but all load functions don't accept uri(stream only).

So, how can I load such a window and ShowDialog() it out?

EDIT

I've read this already, I didn't try it because my thinking is: if I have a absolute URI, why I need to reflect that? waiting for better answer, thanks.

EDIT 2

Sorry guys, I don't need to exactly call PrintWindow class, Window class is enough because all I need to do is call window.ShowDialog() method.

Upvotes: 2

Views: 566

Answers (3)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

Simply loading the XAML is not enough to create a WPF window. You also need some code behind that initializes stuff and contains the program logic (the window class).

What you actually need to do is to (either statically or dynamically) load the respective assembly and instantiate the Window class.

Upvotes: 1

Dennis
Dennis

Reputation: 37760

According to your comment:

all assemblies are in one same solution, so mine. but the problem is my assembly is referenced by UI layer(in my code case, ExternalAssembly), so i cannot call anything out from that assembly

I can say, that passing URI is very indirect way to solve this task. While you could parse pack URI and get assembly and type info from it, the easiest way is to pass window from calling assembly or declare some factory/provider inside your assembly, if you want to decide yourself, when to create window:

public interface IWindowFactory
{
    Window CreatePrintWindow();
}

public void YourMethod(IWindowFactory windowFactory)
{
    // ...
    var window = windowFactory.CreatePrintWindow();
    // ...
}

Upvotes: 1

Jai
Jai

Reputation: 8363

Isn't it as simple as this?

Window window = new ExternalAssembly.Window1();
window.ShowDialog();

Edit

The assembly's project must be part of the solution or be in the reference list.

Upvotes: 1

Related Questions