so I want in my Project to only open 1 instance of my Window. So I gave the Window a Title and tried to track every opening with that:
foreach (Window window in Application.Current.Windows)
{
if (window.Title == "QUALI-NET")
{
temp++;
}
}
and then i wanted to call my Function when this if statement is true:
if (temp == 1)
I have build this 2 in an extra Class and have an Switch Case around this. Above the Switch Case I initiliaze this:
QualiWindow WPFQuali = new QualiWindow(Mandant, Data.GetValue<string>("Artikelnummer"));
But The problem when I Open one Window and then open and another Window then it wont open but when I close the first started Window, I cant open the Window ever Again? I just want to allow one Instance of this Window to open. What am Im doing wrong?
I already tried the solutions from here:
How can I make sure only one WPF Window is open at a time?
But none of that is working. Is there a Way to get every opened Window from taskbar or something and just allow one Window with that name XY. to open
If you're keeping a count of the open windows with that title you also need to decrement the count whenever you close the window.
Related
I have a UWP app written in C#, where I need to open a second window.
Inside this second window, there is an element that I need to toggle the visibility of (in some cases).
Please find an MVCE here: https://github.com/cghersi/UWPExamples/tree/master/AppWindowAndVisibility
If you look into it, you will see an element m_searchGrid which we can change the visibility (using method SwitchSearchBarVisibility())
To open a new window, I use the primitives AppWindow.TryCreateAsync and ElementCompositionPreview.SetAppWindowContent.
Now, the following procedure results in an Exception when calling ElementCompositionPreview.SetAppWindowContent:
Open a new window (in the app, click on the button "Open AppWindow")
Change the visibility of an element in the newly opened window (in the app, in the new window that is opened, click on the button "Show/hide search bar")
Close the second window
Reopen again another window (in the app, re-click on the button "Open AppWindow")
From a code perspective, at step 4 I can see that the method ElementCompositionPreview.SetAppWindowContent throws an Exception (see line 108 of MainPage.xaml.cs, in the method ShowAsync()).
Do you have any clue on what could be the cause?
Maybe I should configure the window or the element in different ways?
Thanks!!
UWP Cannot open an external window after changing visibility of an inner element
The problem is m_wrapperCanvas was used by previous m_appWindow, it was not released even you called m_appWindow = null.
For solved this problem we suggest set m_wrapperCanvas = null, and re-new the m_wrapperCanvas before ElementCompositionPreview.SetAppWindowContent
private void AppWindow_OnClosed(AppWindow sender, AppWindowClosedEventArgs args)
{
m_appWindow.Closed -= AppWindow_OnClosed;
m_appWindow = null;
m_wrapperCanvas = null;
}
..........
try
{
Init();
ElementCompositionPreview.SetAppWindowContent(m_appWindow, m_wrapperCanvas);
}
I'm building a Revit plugin. It consists of a dockable pane that (among other elements) has a button. I want to open a new, separate window when a user clicks this button.
At the moment, i create a new Window, but i don't know if that's the right way to go, because now i see two Revit icons on a task bar. I do not have experience as Revit user, i'm new to Revit development, so i'm not sure if this should be the case (two icons) and as silly as it sounds, i do not have admin rights to install random addins and get a feeling of expected user experience.
I create a Window using the following code:
ParametersMissingValueWindow parametersMissingValueWindow = new ParametersMissingValueWindow();
parametersMissingValueWindow.Show();
Based on the understanding of a dockable pane that i have, i think i do not want to create another dockable pane, but just a simple modeless dialog. I wasn't able to find any examples using WPF. Hence, any information whether this is the way to go or help on how to achieve this is highly appreciated.
The Show method takes an optional parent window argument. Specify the Revit main window as the parent window, and your modeless dialogue will be recognised as belonging to the running Revit process. It is accessible from the MainWindowHandle property.
var MyWindow = new MyWindow();
HwndSource hwndSource = HwndSource.FromHwnd(UIApplication.MainWindowHandle);
Window wnd = hwndSource.RootVisual as Window;
if (wnd != null)
{
MyWindow.Owner = wnd;
//MyWindow.ShowInTaskbar = false;
MyWindow.Show();
}
It's not necessary to assign a value to ShowInTaskbar property, but it actually achieves what i wanted to do from the beginning (have only one program open in taskbar), so i left it as part of the solution, but commentted out.
Big thanks to Jeremy Tammik for pointing out the parent property.
You can use WPF to setup a window to use in revit.
MyWPF menu = new menu();
System.Windows.Window wind = new System.Windows.Window();
wind.ShowDialog(); //--> the window shows up and make stuff for revit
if you need the menu to be a dockable one check this source.
Perhaps is not up to date and you will need to adapt the code to the new api.
So we can only have one opened content dialog at a time. This is fine. But in my app there are several possible content dialogs that can be opened, and I would like to avoid making my own variable because I can forget to add it somewhere and the whole app will crash (because trying to open second content dialog throws exception).
So my question is: How to check if any ContentDialog is open?
Note:
I don't want to check for specific ContentDialog.
I would like to avoid creating my own variables.
ContentDialog is shown in the PopupRoot so using VisualTreeHelper.GetOpenPopups() will help you get it.
var openedpopups = VisualTreeHelper.GetOpenPopups(Window.Current);
foreach (var popup in openedpopups)
{
if(popup.Child is ContentDialog)
{
//some content dialog is open.
}
}
Tested accepted answer (by Vignesh) on target Windows 10 build 18362 and find that ContentDialog is never a child of popup. In my case simple check of the count works best:
protected bool IsAnyContentDialogOpen()
{
return VisualTreeHelper.GetOpenPopups(Window.Current).Count > 0;
}
Please feel free to comment if there's any problems with this approach. Thanks.
Out of curiosity, I wonder why I can't show two different instances of FolderBrowserDialog one after the other in the constructor of a Window, but can do it in the Window's Loaded event.
The Example 1 just shows the first dialog (fbd1), and doesn't show the next one.
The Example 2 shows the two dialogs.
Example 1 :
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
using (var fbd1 = new FolderBrowserDialog()) {
fbd1.ShowDialog();
}
using (var fbd2 = new FolderBrowserDialog()) {
fbd2.ShowDialog();
}
}
}
Example 2 :
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
using (var fbd1 = new FolderBrowserDialog()) {
fbd1.ShowDialog();
}
using (var fbd2 = new FolderBrowserDialog()) {
fbd2.ShowDialog();
}
}
}
By the way, I've also tested with WinForms, and this is almost the same.
It doesn't work in the Form's constructor and in the Form's Load event, but works in the Shown event.
The answer you like is not in fact the correct answer, it actually does activate the second dialog. Activation state and Z-order are distinct windows properties. You just can't see the dialog because you lost the foreground. One you can only ever keep when you have a window that can stay in the foreground.
A program gets ~6 seconds to steal the foreground with its own window after it starts up. That timeout is easy to see, Windows displays the Cursors.AppStarting cursor (small arrow with hourglass). That worked to get the 1st dialog into the foreground. What happens next is however doomed to go wrong. When the user closes the dialog then your app has no window left that can be moved into the foreground. Windows now goes hunting for another window to put in the foreground, inevitably one that's owned by another process. Pretty likely to be the VS main window when you debug for example. And the 6 seconds has expired. The 2nd dialog will show up and get activated but of course it is overlapped by that window.
Cold hard fact is that a dialog must always have an owner. FolderBrowserDialog is a bit too forgiving about that, providing you with a ShowDialog() overload without an owner argument. Very convenient, not always correct. It uses GetActiveWindow() under the hood to find an owner. If there isn't one then the desktop window becomes the owner, trouble ahead,
otherwise without throwing an exception.
As Reza Aghaei said in his 2nd comment :
When you close the first dialog, the second one appears, but since
your Form is not visible at the moment and is not visible in task-bar,
it doesn't activate the second dialog, while it's open behind other
windows. Just press Alt+Tab to see open windows and you will see the
second dialog too. But when your Form is visible (for example when run
code in Shown) you will not have this issue.
This is the answer to my curiosity.
I have a VS2010 Isolated Shell application and I have a tool window as below
public class MyWindow : ToolWindowPane
And this tool window supports multiple instance
[ProvideToolWindow(typeof (MyWindow ),
MultiInstances = true,
Style = VsDockStyle.MDI,
Transient = true)]
public sealed class MyVsxPackage : Package
Then in my application I can open more than one tool window (each tool window has its own window Id), but when I close one of them I just want to hide it, and next time when I reopen the window the settings on the window should keep the same before it was closed, how to handle this?
In another words, my question is: is there any way that prevent the multiple instance tool window from being closed by click the 'x' button (just hide it)?
Thanks in advance!
I supposed you have a list of toolbars with toolbars created in your App.
You must have a method in your toolbar that hides/show it. (If you don't really close toolbars, don't call close method).
When you "close" a toolbar call hide method, when reopen call show. (When close App, call close).
Hope this help you.