Consider the following code:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("MyMessage");
}
If I am trying to display a message box after a WPF window has been loaded, when I run the application, the WPF window is displayed with a transparent background (only the non-client area is visible) and it takes 3-5 seconds until the message box appears. The WPF window returns to normal only after the message box has been closed.
Is this normal? Does anyone else experience this?
EDIT: I have added a screenshot of how the window looks like:
The MessageBox is getting shown at the Normal DispatcherPriority, which occurs before things like DataBind, Render, and Loaded, so the code that initializes your Window's objects is not getting run until after you dismiss the MessageBox
You can fix this by simply showing the MessageBox at a later DispatcherPriority, such as Background
private void Window_Loaded(object sender, RoutedEventArgs e)
{
InitializeComponent();
this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
new Action(delegate() { MessageBox.Show("MyMessage"); }));
}
Try this overload of Show method, or any other overload, accepting Window instance as parameter.
Related
I would like to display a kind of window when I move the mouse over a ListView Object. When the mouse leaves the object, the window should close again.
Does anyone have some tips for me?
Of course that did not work that way. In addition, the window should go to the mouse and not somewhere.
Test:
private void ListViewBilling_MouseEnter(object sender, MouseEventArgs e)
{
_billingInfoWindow = new BillingInfoWindow();
_billingInfoWindow.ShowDialog();
}
private void ListViewBilling_MouseLeave(object sender, MouseEventArgs e)
{
_billingInfoWindow.Close();
}
The window closes and opens continuously.
How do I get a window (popup) to be displayed only when the mouse is moved over a ListView object? Not everywhere in the ListView.
You should use ToolTip or Popup controls. Details here:
WPF ToolTips - MSDN
WPF Popups - MSDN
Understand the differences and choose the best you need. Main differences are:
ToolTips should not be hoverable, just some extra information
Popups are separate windows, they can get custom, clickable items (eg. buttons)
Popups can be opened on hover as well.
What is happening is that the dialog box opens which causes the mouse to leave the billing window which causes the dialog box to close. Then the mouse re-enters the billing window which opens the dialog box, etc., etc. This is causing the loop you are seeing.
I suggest that you adjust and have the dialog box close when the mouse leaves the dialog box.
My current solution looks like this:
private void Show_PopupBillingPreview(object sender, MouseEventArgs e)
{
var listViewItem = e.Source as ListViewItem;
var billing = listViewItem?.Content as Billing;
PuBillingPreviewTitle.Text = billing?.BillingId.ToString();
PuBillingPreview.PlacementTarget = listViewItem;
PuBillingPreview.Placement = PlacementMode.MousePoint;
PuBillingPreview.IsOpen = true;
}
private void Hide_PopupBillingPreview(object sender, MouseEventArgs e)
{
if (PuBillingPreview.IsOpen)
PuBillingPreview.IsOpen = false;
}
Now I have the problem that I have a flicker when the popup window appears directly under the mouse.
I began using C# and WPF a few months ago and now I thought I'd try to learn some new techniques like using threading. So I have an app that I want to run all the time (using an infinity while loop) but show the dialog (main window) every minute. So I am doing this by using Threading and here is how i am doing this:
public MainWindow()
{
InitializeComponent();
while (true)
{
callmyfunction()
system.Threading.Thread.Sleep(1000);
}
}
In my callmyfunction(), I am calling the dialog (which is the main WPF application) so it will show. Here's how i am doing it:
public void callmyfunction()
{
this.ShowDialog();
}
I have a regular button and when you click on it, it should hide the main window. So my button function is like this:
private void Button_Click2(object sender, RoutedEventArgs e)
{
this.Hide();
}
So what I am doing is, I am loading the main window normally and it has a button, when I click on that button, it should hide the main window and the window should sleep as per the milli-seconds I specified in thread.sleep and then it will wake up, then again the dialog will appearand it will show the button and so on and so forth. This loop is working fine with me, but the issue I am having is that after the first dialog appears and I click on the button to hide the main window, the second time the main window appears, the button would appear as a "pressed" button, not as a new button. I think it's because I "pressed" on it the first time the main window appeared. And it stays like that until I stop the program and run it again.
So my question is, any idea how I can "reset" the button control? Or do I need to reset the mouse click event? Any pointers on this would be helpful.
You should run the loop on a background thread. If you run it on the UI thread, the application won't be able to respond to user input.
The easiest and recommended way to run some code on a background thread is to start a new Task.
Also note that you cannot access the window from a background thread so you need to use the dispatcher to marshal the call back to the UI thread.
The following sample code should give you the idea.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Task.Factory.StartNew(() =>
{
while (true)
{
Dispatcher.Invoke(() => callmyfunction());
System.Threading.Thread.Sleep(5000);
}
}, TaskCreationOptions.LongRunning);
}
public void callmyfunction()
{
WindowState = WindowState.Normal;
}
private void Button_Click2(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
}
I try to show the authentication window, then open the main window,
but when you close the authorization window, application is stopped
private void App_OnStartup(object sender, StartupEventArgs e)
{
new LoginWindow().ShowDialog();
new MainWindow().Show();
// Then application stopped
}
BUT!
If the display window authentication by using method Show(), the application does not close after closing the authorization window
private void App_OnStartup(object sender, StartupEventArgs e)
{
new LoginWindow().Show();
new MainWindow().Show();
// Then application running
}
Why is this behavior???
Thanks to Eran Otzap!
Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
Is working!
By default, when the main windows of the application is closed, then the application is closed.
According to the documentation, "Application.MainWindow is automatically set with a reference to the first Window object to be instantiated in the AppDomain."
To get around this, you can try to create first a MainWindow object (without calling Show()),
then create and show the login dialog, and then show the main window.
I want to hide my form while keeping my application running in background.
I've used notifyIcon and it remains always visible.
I've used "this.Hide();" to hide my form but unfortunately my application gets close (no exception).
I am also using threading and this form is on second thread.
Please tell me how can I solve it.
I am also using threading and this form is on second thread.
My crystal ball says that you've used ShowDialog() to show the form. Yes, calling Hide() on a modal dialog will close it. Necessarily so, a modal dialog normally disables all of the windows in the application. If you hide it then there's no way for the user to get back to the program, there are no windows left to activate. That this form runs on another thread otherwise doesn't factor into the behavior.
You'll need to call Application.Run(new SomeForm()) to avoid this. Now it isn't modal and you can hide it without trouble. But really, do avoid showing forms on non-UI threads. There's no reason for it, your main thread is already quite capable.
add the following event handlers for form resize and notify icon click event
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
but this is not close you application
When WPF window appear first time, its content seem frozen. To refresh content I need to resize form, then it will be fixed. Or I hit the TAB then find a listbox -it's not visible- and click it and viola! Form updates its content again.
What do you think? Weird huh? Thanks in advance!
Edit:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Show();
while (!AppMain.needClose)
{
System.Windows.Forms.Application.DoEvents();
DoThings();
}
}
Resizing the window would force the internals to invalidate and re-paint. You could try invalidating the form when it's loaded to force it to do the same:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Show();
this.Invalidate();
while (!AppMain.needClose)
{
System.Windows.Forms.Application.DoEvents();
DoThings();
}
}
Unless you're doing some sort of custom message pumping though, the standard forms message pump should do that while loop for you. You might well find that because you're intercepting the window loaded event you're stopping initialisation from completing.
Calling DoEvents is a bad code smell in my experience. If you need to do something periodically, it's better to trigger it from a timer of some sort.
When your window is loaded it's already shown, why are you calling this.show() again??