I am unable to shutdown my WPF application on closing the main window. Just facing this issue after adding new window (splash screen) to my project.
Below is the newly added code in App.xaml.cs
SplashScreen splash = new SplashScreen();
splash.Show();
MainWindow main = new MainWindow();
this.MainWindow = main;
Thread.Sleep(2000);
splash.Close();
Try to set the Shutdownmode of the App to ShutdownMode.OnMainWindowClose:
ShutdownMode = ShutdownMode.OnMainWindowClose;
Then the app should shutdown when you close main (assuming you actually show it first).
Related
I'm using Prism's MefBootstrapper to initialize my WPF application, as part of this process the Shell window is being initialized.
After Running the MefBootstrapper's Run() method, I'm displaying a login window which connects to a server and in case of a successful connection, the Shell is loaded (from main UI thread).
This login window is running on a new dedicated UI thread, in order not to freeze the progress-bar in this process (connection to the server and especially the Shell loading).
Unfortunately, the login window is not showed on the top and not Focused (even when I'm using: Activate(), TopMost=true, Focus() ).
//UI mode - a new UI thread is initialized
var uiThread = new Thread(() =>
{
var loginWindow = new LoginWindow();
loginWindow.Show();
loginWindow.Activate();
loginWindow.Topmost = true;
loginWindow.Topmost = false;
loginWindow.Focus();
System.Windows.Threading.Dispatcher.Run();
});
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.IsBackground = true;
uiThread.Start();
How can I solve this issue?
Try uiThread.IsBackground = false;
Can you send new instance of LoginWindow to the thread?
uiThread.Start(new LoginWindow());
and then use the loginWindow parameter in the Thread delegate to Show.
I guess that would cause the window to render in the current dispatcher context and would render it on the foreground.
I would create the loginWindow before the ShellWindow is created, just before the end of ConfigureContainer().
That way you won't unnecessarily create views - viewmodels in your regions.
Note, it is important that you don't Close the window before you have shown your ShellWindow (i.e. hide it on successful login) or else the Application will close too.
I have an MFC MDI application and I am trying to add a new dialog to it. I want this dialog to be in WPF (a Window basically rather than a dialog). This window should be modeless and a child to the current MDI View.
Let's say I have CMyView in the MFC application, and in its OnCreate, I try to create the WPF Window. To do so, I made a wrapper class called CMyWindowWrapper (that compiles with /CLR)
int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
m_wrapper.Create(this);
return 0;
}
The window wrapper class has a Create function which actually creates the WPF Window:
void CMyWindowWrapper::Create(CWnd* pParent)
{
MyWindow^ window = gcnew MyWindow();
window->ShowModeless((IntPtr)pParent->GetSafeHwnd());
m_myWindow = window;
}
MyWindow is the WPF Window where I added a function called ShowModeless as follows:
public void ShowModeless(IntPtr parent)
{
WindowInteropHelper helper = new WindowInteropHelper(this);
helper.Owner = parent;
Show();
ShowInTaskbar = false;
}
Now the application behaves as follows: whenever a CMyView is created, a modeless MyWindow is created successfully, and it appears always on top of CMyView even if the focus is on CMyView. However, when CMyView is closed or minimized, MyWindow is not following it. It gets close/minimized only if the whole application gets closed/minimized.
I can attach a sample application showing the problem if needed.
Please advise.
Thank you so much.
An alternative solution would be to make your WPF window a user control. Create a MFC modeless dialog and put the WPF user control in the MFC modeless dialog.
I'm writing in wpf. In my viewModel I have a command that opens new window. However sometimes this child window is placed under the parent window. (if for instance I work in my application, then open browser and want to return to my application). Window is opened as follows:
MyViewModel vm = new MyViewModel(oper);
Mywindow window = new MyWindow();
//Initialize viewModel and set window.DataContext to this viewModel
CWPFWindowWithViewModelHelper<IWindowWithViewModel>.InitializeViewModel(window, vm);
window.ShowDialog();
I want this child window to be always visible when opened. How can I do this?
just try with
window.Owner=this
window.TopMost = true;
i'm developing an application (wpf) that have 3 windows. in the main window user can open other window. when user wants to open window1 i'm create an instance of window1 and showing up that
var win1 = new Window1();
win1.Owner = this;
win1.Show();
now when user wants to close app, i want to iterate through each open window and check if that window isn't busy (or if is busy wait to complete operation) close that window and then close application. my question is how to iterating through open windows? maybe using this:
foreach (var window in Application.Current.Windows)
{
window.
}
but how i can detect window is Window1 or Window2?
You can do
if(window is Window1)
{
//window is of type 'Window1'!
}
Inside the 'foreach' loop.
I have a WPF project with some styles in XAML in Application.Resources. This works perfectly fine. But when I open a window from this project from another one (this one is a console application), the resources from XAML aren't loaded. When I first tried it, I got a XamlParseException on StaticResource calls in XAML, so I changed it to DynamicResource and now the style just doesn't get loaded. How do I fix this?
The code I use:
[STAThread]
static void Main()
{
App app = new App();
MyWindow wnd = new MyWindow ();
wnd.Show();
app.Run();
}
You should call the Run method that takes a Window parameter. In your current code, you're creating and showing the window before running the app, which means the application resources aren't loaded yet.
Try:
App app = new App();
MyWindow wnd = new MyWindow();
app.Run(wnd);