I have a Xamarin/MonoMac project with 2 windows. The main window opens, and functions like it should. However, I have a button that needs to open a second window (Lets call the first window "Main", and the second window "Second"). Both have their respective XIB files, and controller classes.
The button is set up in MainWindowController, and functions normally such that:
secondButton.Activated += (o, e) => {
//Do stuff
}
Methods such as Console.WriteLine(); function as expected when the button is clicked.
The second window controller class is set up such that:
public partial class SecondWindowController : MonoMac.AppKit.NSWindowController {
...
}
What I need to do is cause secondButton to open an instance of SecondWindow upon being clicked, while passing the active instance of MainWindow to the SecondWindow constructor.
In C#.NET I would just use
SecondWindow sc = new SecondWindow(this);
sc.ShowDialog();
I can get SecondWindow to open, using this
NSWindowController nsc = new NSWindowController("SecondWindow", this);
nsc.ShowWindow(this);
But that causes an error stating
"Unknown window Class SecondWindow in Interface Builder file, creating a generic window instead."
The window shows as it should display, but no functions in SecondWindowController function, even if I run
nsc.AwakeFromNib();
directly after opening the window. No console output, and none of the buttons work.
How might I get the window to open, and function properly? If more information is needed, please let me know what I need to add, so I can get that to you.
Thanks in advanced!
To get the window from the Storyboard you would need to use NSStoryboard.InstantiateControllerWithIdentifier(string id) as NSWindowController
Also you must set id as identifier for the window in the storyboard.
Related
I have an app that consists of 2 parts. 1st part is Login form, where user needs to enter login and password. If they are correct, it start "Editor" window where user can work.
For now in order to launch second window I use:
var editorWindow = new EditorWindow();
editorWindow.Activate();
The problem is that Login window is still there, and while it is not critical, I still want to close it after Login is done.
First time I tried to add Window.Close() after opening the 2nd window in the .cs file of 1st Window, so
var editorWindow= new EditorWindow();
editorWindow.Activate();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted Attempted to read or write protected memory eror.
I tried to do it in the 2nd Window .cs file like this:
this.InitializeComponent();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted the same error
So how can I do this properly?
If you open the second window in the code-behind of the first window, you should be able to just call this.Close() right after you've called Activate() on the new window:
var editorWindow= new EditorWindow();
editorWindow.Activate();
this.Close();
If you open the EditorWindow from somewhere else, you need to get a reference to the first window to be able to close it. You could for example use a variable in the App class for this as suggested here.
In my program I have two windows, the first one being my main window with a text box and the second one having an entry field with a button to update the text box in the first window. I'm a beginner in terms of using WPF and coding in C# in general, but is there a way to pass a pointer or reference of my main window to the second window so the second window can edit the text box of my first window? Is that even the right way to think about solving this issue?
WPF assumes you are binding your forms to a ViewModel object. This object can be bound to more than one form to give you different views and capabilities, so in this case you'd bind the same ViewModel to both forms, and what is changed in your edit form will appear automatically in your main form.
Your question is a bit vague and there are many approaches to accomplishing this. MVVM as Steve Todd mentions, is one.
However, it sounds like you simply want to open the window as a dialog. In your second window's code behind, be sure your textbox has a name in XAML and then access it create and easily accessible property that gets and sets your textbox value.
public MyTextContent
{
get => this.MyTextBox.Text;
set => this.MyTextBox.Text = value;
}
You can control the return value based on conditions (such as OK or Cancel buttons) if you like by using click events. The window contains a DialogResult property. The default is false, so you will need to set this somewhere.
this.DialogResult = true; // OK
Then in your main window's code behind, create a new instance of the window, assign it's property and show it. This will need to be done during a click event of a button or some similar trigger
var myDialog = new MyDialogWindow()
{
MyTextContent = "Textbox Starting Value";
}
bool? result = myDialog.ShowDialog(); // Returns when the dialog window is closed.
if(result != null && result)
{
this.LocalTextBox.Text = myDialog.MyTextContent; // Copy the text to the main textbox.
}
Typically you do this in data context of your main window. You use IoC to pass an instance of popup notification service in the constructor and create a private reference. You call that service method that displays the popup notification where user can enter async (and await) for its response or use reactive extensions to subscribe to submit action of that button. A thing to look out for is that you can update ui only in dispatcher thread and do not forget to dispose the subscription after you have finished using the window.
My program has a MainWindow and a SecondWindow, which is called by the first one like this:
SecondWindow config = new SecondWindow();
config.Owner = this;
config.Show();
Those lines are contained on a Button.Click method. And I want to check if it is already open, close it or do not open it.
Thanks!
Do not create a new instance. Just add it to top of your MainWindow class, and when you click the button, use secondWindow.Hide();. You must hide, because if you close it, you can't show it again. If you want to do not open it, activate the window and take it to top of desktop with secondWindow.Activate();.
Try this:
if(Application.Current.Windows.OfType<SecondWindow>().FirstOrDefault() == null)
{
//second window not exist
}
I'm pretty new to WPF and I'm trying to make a database system. What I currently have is a Login Window. When you enter the user and password you are supposed to go to another window StudentInfoSystem . The code I used is pretty basic and common.
var info = new StudentInfoSystem.MainWindow();
info.Show();
this.Close();
So, what this would do, is after you press the login button, you get transferred to StudentInfoSystem and the login window closes. The problem I have with Show() is that it opens a window and immediately returns, right? It doesn't wait for the new window to close. So, my question is how can I open a new window and WORK with it? When I say work, I meant to show out information in my textboxes (In the NEWLY opened window) depending on the role the user has, etc...
I'm guessing the above code is in the button click handler for the Login Window, which would make the Login Window the parent of the StudentInfoSystem window.
Since WPF will close the parent and any child(ren) window(s) when closing the parent window, your StudentInfo window will also close when calling
this.Close();
One option might be to instead call
this.Hide();
but without seeing how the rest of your app is set up, not 100% sure this is the best approach.
Perhaps see these SO questions:
wpf-create-sibling-window-and-close-current-one
how-to-close-current-window-in-code-when-launching-new-window
Try window.Activate() to focus the new window and/or [any element].Focus() to focus any element within window.
As I understand, this should do what you want:
info.ShowDialog();
You could also check ShutdownMode property. I would rather say, login window is sth you want to close after login, but do what you want :). Usage of ShutdownMode property:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
this.ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
}
}
In the program I am trying to build, I have a menu button that opens a second window. The user puts information into the second window, presses the "Done" button, and the information is transfered into the main window. The problem I am having is opening the second window. I have both windows build in xaml files in Visual Studio but I can't find a way to show the second window. Using "Window window = new Window" does not fit my needs because 1) I already have the second window built and 2) I have tried this and I cannot figure out how to add children to the window; there is no window.children nor any grid to put the children into. Thank you in advance!
Moments after I pressed post, I thought of something I hadnt tried:
"WindowAdd add = new WindowAdd; //WindowAdd being the second window
add.Show();"
This does exactly what I want it to do. The next problem I have is sending the information the TextBoxes into the MainWindow. I am thinking cookies might work but am unsure. Anyone have any thoughts? Thanks in advance!
You need to create the Window in code, but instead of doing:
Window window = new Window();
You should use:
Window2 window = new Window2(); // Assuming the window's class name is Window2
This will construct and initialize an instance of your new window class, defined in XAML. Once you've done this, you can open the window and you'll see all of your controls.