I have two wpf windows in my wpf application.
1) When i click on the load button it loads the second Window. The secong window takes 15 to 20 secs to load.
How do i add a progress bar to show a loading window and when the second window loads close the progress bar.
I was recently working on a loading window for my app where you click the app and it takes about 10s to load. I have a loading window with an intermediate loading bar. The key was to put the loading window in a different thread to enable the animation to run while it was loading the other window on the main thread. The problem was to make sure we do stuff propertly (like when we close we close the window should stop the thread... etc).
In the code below... LoadingWindow is a small window with a progress bar on it, SecondWindow would be the window that is slow to load.
public void OnLoad()
{
Dispatcher threadDispacher = null;
Thread thread = new Thread((ThreadStart)delegate
{
threadDispacher = Dispatcher.CurrentDispatcher;
SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(threadDispacher));
loadingWindow = new LoadingWindow();
loadingWindow.Closed += (s, ev) => threadDispacher.BeginInvokeShutdown(DispatcherPriority.Background);
loadingWindow.Show();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
// Load your second window here on the normal thread
SecondWindow secondWindow = new SecondWindow();
// Presumably a slow loading task
secondWindow.Show();
if (threadDispacher != null)
{
threadDispacher.BeginInvoke(new Action(delegate
{
loadingWindow.Close();
}));
}
}
There are many ways you can accomplish that. An easy way would be to create a third window or panel with the progress bar or a waiting animation. That third window is in charge of loading your second window and get displayed as soon as you click the load button on the first window. When loading of the second window is completed, the third window with the progress bar closes and the second window gets displayed.
hope this helps.
You could use the BusyIndicator as part of the WPF extended toolkit. You can download it here: http://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator
On immediate loading of the new window before you do your expensive processing that takes forever, you can set the IsBusy to true. When processing is done, set IsBusy back to false. This method involves wrapping your XAML in the BusyIndicator in the 2nd window, which may or may not be what you want.
Related
I was creating a browser window for my WPF application. During the visibility change I am loading the URL, but when I change the URL content and load the same URL it is showing previous content first, then after a blinking it shows the updated data.
void OnWindowVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (!IsBrowserInitialized) return;
if (window.Visibility == Visibility.Visible)
{
Dispatcher.BeginInvoke(new Action(() =>
{
LoadNewUrl();
window.Activate();
}));
}
else
{
Dispatcher.BeginInvoke(new Action(() =>
{
ChromeBrowser.LoadHtml("<html><body><h1></h1></body></html>");
}));
}
}
Suppose the URL contains "ABCD" as data. First time it is showing the correct content. After the update the data becomes "ABCDEF". Now on Load() the initial content is showing then the updated content.
Is there anything else I need to do for avoiding this issue?
I'm not sure what LoadHtml() does, it's not a part of the native CEF API. In any case the blank page is about:blank, i.e. ChromeBrowser.Load("about:blank").
Are you aware of stopping all activities in the hidden browser? It seems to be the issue - you load an empty page and hide the window, the browser stops any activities, then you activate the window and still see an old content, then an empty page causes "blinking", and finally a new content is shown.
You should catch change visibility request, cancel it, load about:blank and hide the window on load completion. The client handler has appropriate events.
Currently from my application I'm firing off a modeless window. This new window will show on top of the main application. I would like to give my users the ability to just click on the main application which is just behind the popup and bring it to the front. Right now the user would have to minimize the popup to access the main application. Here is what I'm essentially doing:
Window dialog = new Window
{
Content = new Control(data),
Owner = Application.Current.MainWindow,
ShowInTaskbar = true,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
dialog.Show();
Any help would be greatly appreciated.
Take out this line:
Owner = Application.Current.MainWindow
and you should get the behavior you want. By specifying the owner window you are keeping the child window always in front.
In method that is working in the background, i have two important lines :
createPopup();
MessageBox.Show(sth);
more lines
more lines
createPopup() just creates a popup, adds a grid as a child and shows popup.
My question is, why first shows up messageBox, then shows up Popup, which appears after all lines in this method done ? How could I make this popup to show before all lines in this method will be done ?
All the UI changes are normally queued up and will be shown at once on the screen.
And this does not include MessageBox. So it shows up immediately and prevents the execution, until user clicks on Ok. Hence eventhough your popUP is first executed, it will be shown in the UI only after the MessageBox.
For your problem, Try placing your MessageBox.Show(something) in a separate thread.
createPopup();
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("some message");
});
more lines
more lines
Give it a try. I am not sure whether it solves your problem or not as I dnt know the code in createPopUp() method.
Creating the pop-up, does not actually draw it on the screen until the Layout event. If you want to ensure that the pop-up has been drawn before you display the pop-up, attach an event handler to the pop-up's LayoutUpdated event and display the message box from within that event handler. Be sure to detach the event handler as well or you will see multiple message boxes.
public InitPage()
{
Popup popup = new Popup();
popup.LayoutUpdated += popup_LayoutUpdated;
LayoutRoot.Controls.Add(popup);
}
void popup_LayoutUpdated(object sender, object e)
{
popup_LayoutUpdated -= popup_LayoutUpdated;
MessageBox.Show("hello");
}
I'm writing small notification component, however I have a problem with showing multiple notification at once.
My alert/notification window inherits from window class. The contstructor of Alert looks like that
public Alert()
{
InitializeComponent();
Focusable = true;
ShowActivated = false;
ShowInTaskbar = false;
//Topmost = true;
AllowsTransparency = true;
Opacity = 1;
// Set up the fade in and fade out animations
_Hint = "hint";
Loaded += new RoutedEventHandler(DesktopAlertBase_Loaded);
}
In main window of application I have a list of alerts, thanks to this I know where to place alerts on the screen. If I want to show an alert I create an instance of Alert class and then I use Show() method. Alerts are shown and everything is OK except the fact that I can use/move/interact with only last alert window (last created window).The rest of alert windows can't be clicked. However if I close last window I can use one before last and so on... Is it possible to make multiple windows clicable/active ?
Can you please, provide code, where you use show method? It looks like you are using ShowDialog() method instead of Show().
Another possibility is some locks in your DesktopAlertBase_Loaded() method.
New Thought, Maybe I am looking at this totally incorrectly. So Here is exactly what I am trying to do in case there is another option I am not aware of.
I have a WPF app, the main window shows a smaller dialog window using ShowDialog(), when a user clicks on the parent window that showed the dialog, I need to make the dialog window, flash, shake or blink.
AresAvatar posted a link that looks like it might be able to use, but is there another option I am not aware of?
My original Question.
Mouse click event when Modal window's parent is clicked in WPF app?
I have a wpf app that shows a modal window using ShowDialog().
I would like to fire an event when the user tries to click the parent window that is now disabled.
Is it possible on the parent to receive a click event when it has shown a modal window?
When I attempted this using an interaction trigger, the events never fired on parent window.
Otherwise, what suggestions / options are there.
Thanks
No WPF events are sent under these conditions. The only Windows message I can see that gets sent is WM_WINDOWPOSCHANGING. You could check for that message, and check if the window was disabled when it occurred. Here's a good article on checking WM_WINDOWPOSCHANGING.
Edit: that link seems to be dead. Here's an example on StackOverflow of checking window messages.
I know this is an old question but I'll post my solution in case any one needs it.
Set the dialog.owner prior to calling ShowDialog().
var dialog = new DialogWindow();
dialog.owner = MainWindow;
dialog.ShowDialog();
The result is that clicking on the main window, brings the dialog window to the front and makes the dialog window flash.