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
Related
I'm studying to become a developer; as a formative project, I'm working on a desktop app to help me organize local amateur chess tournaments with MAUI. Basically, I want to be able to open multiple windows during the same execution, so that I can run various parallel tournaments on the same machine.
On the MainPage I placed a button that creates a new window that displays the NewTournamentPage
private void NewTournamentButton_Clicked(object sender, EventArgs e)
{
Application.Current.OpenWindow(new Window()
{
Page = new NewTournamentPage()
}) ;
}
in the NewTournamentPage I placed a button to add a new player to the tournament, and I want the window to freeze until the user inserts the player name, but without blocking the execution of the other windows. DisplayPromptAsync seems to be exactly what I'm looking for, so I did this:
public async void AddNewPlayer_Clicked(object sender, EventArgs e)
{
string newPlayerName = await this.DisplayPromptAsync("Add new Player", "Name:");
//code to add player to tournament
...
}
When I execute and click the NewTournamentButton multiple times, the windows are created with no problems, and they all work independently from each other, but when I press the AddNewPlayer button, the popup pops on the MainPage window, not on the one calling the method, and it takes and saves the input in newPlayerName; then, the same thing happens on all secondary windows, in order of creation, without updating newPlayerName. What is happening here? What am I missing?
Yes, it is just the case as you said. And I have created a new issue about this problem on github.
You can follow it up here: https://github.com/dotnet/maui/issues/7650.
Thanks for your feedback and support for maui very much.
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.
I want to create an autologin WinForms app with TestStack.White. Here is my code:
private void button1_Click(object sender, EventArgs e)
{
Process[] process = Process.GetProcessesByName("XYapp");
TestStack.White.Application app = TestStack.White.Application.Attach(process[0].Id);
TestStack.White.UIItems.WindowItems.Window window = app.GetWindow("XYwindowName", TestStack.White.Factory.InitializeOption.NoCache);
TestStack.White.UIItems.Panel panel = window.Get<TestStack.White.UIItems.Panel>(TestStack.White.UIItems.Finders.SearchCriteria.ByText("Login"));
panel.Click();
...
}
The XY app main window has 10+ panels and one of them is the "Login" panel. When I click the button1, the XY app "Login" panel will be visible, so it works.
But my WinForms app freezes and I get the control back when I close the XY program but I want to run further.
In debug mode, the following error message is displayed:
"Managed Debugging Assistant 'DisconnectedContext' : 'Transition into COM context 0x15305d0 for this RuntimeCallableWrapper failed..."
I think I understand why I get this message, but I have no idea what the solution is.
Someone could help me?
thanks
I'm sorry for the stupid question, the solution is simply just make a new thread.
https://learn.microsoft.com/en-us/windows/desktop/winauto/uiauto-threading
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();
}
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);
}}