Open WPF prism app from win forms - c#

We're working on a quite large application partly written in Win Forms. From some time we're rewriting some windows to WPF and we're calling it in this way:
SomeWindow window = new SomeWindow(someParameters);
window.ShowDialog();
Recently we wanted to use PRISM and MahApps in a similar way, but without success.
So I've tried the following:
private void button1_Click(object sender, EventArgs e)
{
var app = new SomePrism.App();
app.Run();
}
This is starting the app, but if I want to open it again I'll receive following exception:
Cannot create more than one System.Windows.Application instance in the same AppDomain.
I've tried using answer from Multiple WPF applications in the same AppDomain but without success (when I'm trying to open WPF app again, the Application.Current is null, but exception still occurs).
I would also like to 'suspend' the win forms until user is finished working with WPF like it's happening when you show a dialog.
Have someone any idea if this is possible and if it is, how this could be achieved in a proper way?
EDIT
I've found a solution. Instead of using InitializeShell inside Bootstrapper like below:
protected override void InitializeShell()
{
Application.Current.MainWindow.Show();
}
I've written it in the following way:
protected override void InitializeShell()
{
var window = Container.Resolve<MainWindow>();
window.ShowDialog();
}

Related

How to create Module Catalog and Reigster Types without opening the MainWindow in Prism

I'm writing a dll for another application and I want to implement my own button in this application which would open a dll which, in turn, would open WPF form.
The question is, when application is being open, I want to do some things for a WPF application which user will open later. For instance, I want to create Module Catalog. In other words, I want to initialize WPF application without opening the Main Window.
Due to the opening WPF from another dll, I create an instance of my Application:
App app = new App();
My App class basically looks like this:
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
protected override IModuleCatalog CreateModuleCatalog()
{
}
}
So I want CreateModuleCatalog and RegisterTypes methods to be completed, but don't want to show the MainWindow from CreateShell method right now.
Could anyoune tell how can I handle this problem?
If you return null from CreateShell(), there is no window to be displayed.
You can also override OnInitialized(). It's where Prism calls MainWindow?.Show() by default.
Having that said, it's a good practice to always display some kind of splash window as soon as possible.

WPF C# Tray Icon Implementation Issue

I've faced the issue while trying to implement a tray icon. App was instantly closing every time I launched it. So first thing first I decided to create a new project to try it there, just in case if I messed something up in my app. But it proceeded to close.
In this new project, I just removed StartupUri and added this piece of code. One more important thing - If we comment the line with Drawing.Icon the window doesn't close after running despite any tray options.
protected override void OnStartup(StartupEventArgs e)
{
MainWindow = new MainWindow();
MainWindow.Show();
Forms.NotifyIcon _notifyIcon = new Forms.NotifyIcon();
_notifyIcon.Icon = new System.Drawing.Icon("icon.ico");
_notifyIcon.Visible = true;
base.OnStartup(e);
}
I would be extremely grateful if you could help me to figure it out
It was very simple. I was trying to import WinForms for WPF.
Here is everything explained about Notifyicon in WPF.
Thanks to Andy

WPF how to change which window opens first

Im using WPF to make a simple budget app for myself as practice and I can't figure out how to change which window opens first. I mean when you start the program , right now it opens the MainWindow, but I want it to open another window. I have tried this in my app.xaml.cs file:
public partial class App : Application
{
void App_Startup(object sender , StartupEventArgs e)
{
GetNameWindow getNameWindow = new GetNameWindow();
getNameWindow.Show();
}
}
which I read was a way to do it but it doesn't work for me. I'm using c# and visual studio 2017. Thanks!
To change the startup window, open App.xaml and replace "MainWindow.xaml" with your window:
StartupUri="GetNameWindow.xaml">
For what you were trying to do, you would need to remove StartupUri="MainWindow.xaml" and instead use Startup="App_Startup" and then it would call your event Handler at startup.

Minimizing Application to system tray using WPF ( Not using NotifyIcon )

I am finished making my application and now I want to incorporate " minimizing into the system tray feature " for it . I read up a good article minimize app to system tray . I realized that these make use of the Windows.Form class .
Unfortunately I have used Windows Presentation Foundation WPF reference to make my applications UI . Now I see that the NotifyIcon is not supported in WPF. I see that there is an open source library on CodePlex that simulates the NotifyIcon properties WPF Contrib I have not used it as yet .
Now I am in a fix . Here are my questions :-
a) I don't want to incorporate a 3'rd party library just for one single component .
b) Can I do the minimizing feature without NotifyIcon on WPF? If yes then how can someone give me leads please ?
Or maybe I should revert my UI back to using Windows Forms ?
If you'll reconsider your reluctance to using an external component, I recommend WPF NotifyIcon. I've used it. It's straightforward and works well.
It does not just rely on the corresponding WinForms component, but is a purely independent control which leverages several features of the WPF framework in order to display rich tooltips, popups, context menus, and balloon messages.
I just came across this post today.
For reference, I also solved this some time back. It works very well and the only time I have had a bit of an issue is occasionally on some multi-display set ups.
This was before GITs and NuGets were the in-thing, I will place it in a GIT repo if there is interest.
CodeProject Article Here
Solution with System.Windows.Forms.NotifyIcon
Here is a thread , which helped me a lot .
https://stackoverflow.com/a/12428063/10305444
public partial class Window : System.Windows.Window{
public Window()
{
InitializeComponent();
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new System.Drawing.Icon("Main.ico");
ni.Visible = true;
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == WindowState.Minimized)
this.Hide();
base.OnStateChanged(e);
}}

Missing images when opening a wpf window from a windows forms application

I am trying to open a WPF-window from a Windows Forms application. The Windows loads, and the functionality of the WPF-window is as it should be, but the images i added to some buttons in my WPF-window do not get shown. The build action property of the images has been set to "Resource". I am loading from the WinForm App with this code:
private void button1_Click(object sender, EventArgs e)
{
var wpfwindow = new WPF.View.MainWindow();
wpfwindow.Show();
}
What can i do to solve the problem?
In WinForms there is no WPF System.Windows.Application active, therefore 'pack' uri prefix is not recognized and resource images referenced as 'pack://some image uri' cannot be loaded.
Either register uri prefix:
if (!UriParser.IsKnownScheme("pack"))
{
UriParser.Register(new GenericUriParser
(GenericUriParserOptions.GenericAuthority), "pack", -1);
}
or try to create an instance of Application class

Categories

Resources