WPF how to change which window opens first - c#

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.

Related

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 Blank Solution with multiple projects sharing same MainWindow

I'm having a Blank solution with multiple projects. I want the Wpfloginwindow to be linked with the other WPF projects (WpfMembers). What I mean is when I click on the connect button in the MainWindow.xaml of the Wpfloginwindow, I want the Mainwindow.xaml of WpfMembers to be shown. How can I do that? I only know how to do that when I'm having multiple windows in one project. But here is different I want to call the WPF window of another project but in the same solution (SlnProject).
An WPF window is a normal class that inherith from Window class. You can just call your member window from your login window, referencing the namespace from your member window:
Main window Login XAML:
<Button Click="Button_Click">Connect</Button>
code behind:
private void Button_Click(object sender, RoutedEventArgs e)
{
var mainWindowMember = new WpfMembers.MainWindow();
mainWindowMember.Show();
}
This is the simplest way to implement it. To improve your project architeture and decouple your business logical from UI, maybe you want to implement MVVM pattern:
https://learn.microsoft.com/en-us/archive/msdn-magazine/2009/february/patterns-wpf-apps-with-the-model-view-viewmodel-design-pattern

How to ShowDialog two windows sequentially with APP.xaml Startup function

private void AppToStart(object sender, StartupEventArgs e)
{
var startUpWindow=new StartUpWindow();
startUpWindow.ShowDialog();
var mainWindow = new MainWindow();
mainWindow.ShowDialog();
}
I have wrote these commands to ShowDialog MainWindow after StartUpWindow.
But when I close the StartUpWindow instance, the entire application closes.
What's wrong with my code?
If there is a better way to doing what I want instead of creating an instance and calling his ShowDialog method please tell me.
You could start by checking your Application.ShutdownMode property to see what it is set to. It can be found in App.xaml.
Per MSDN:
A ShutdownMode of OnMainWindowClose causes Windows Presentation Foundation (WPF) to implicitly call Shutdown when the MainWindow closes, even if other windows are currently open.
after i added ShutdownMode="OnExplicitShutdown" in APP.xaml properties , my app closing with Application.Current.Shutdown(); method.

Open WPF prism app from win forms

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();
}

How to open a window then load my XAML file to it? (XAML and C#)

I am new to WPF and C# and i need some help - sorry about the newbie question!
Anyway I have a control panel 'window' as the file thats loaded when i run my project, and i then have a button on this control panel, that when clicked triggers an event. Inside this 'event function' I am trying to load a new window that has its own XAML code behind, how do i go about doing this? I have googled but to no avail.
Please explain in laymens terms, I am still getting the hang of this all.
Thanks!
private void btnCustomers_clicked(object sender, RoutedEventArgs e)
{
//load in Customers.xaml file here - in a new window
}
You need to declare an instance of the class that is your other window then call Show() on it. So if your other window is call MySecondWindow you write the following in your event handler.
MySecondWindow otherWindow = new MySecondWindow();
otherWindow.Show();
A basic explination of how windows work can be found on the MSDN Site.

Categories

Resources