简体   繁体   中英

Having problems opening a new WPF window with C#

I'm new to C# and I am trying to learn! But I've been stuck trying to get a new window to open! I've read various other similar questions here on StackOverflow and googled but found nothing!

I have two windows. One called MainWindow and another called ChildForm . I use the follow code on a button I named openNewForm .

private void openNewForm_Click(object sender, EventArgs e)
    {
        ChildForm f2 = new ChildForm();
        this.Hide();
        f2.ShowDialog();
    }

From my understanding, this is the standard way of opening a new window or form. I have seen some tutorials use var f2 = new ChildForm(); however. When trying to run my application, I get the following error message.

Severity Code Description Project File Line Error CS0246 The type or namespace name 'ChildForm' could not be found (are you missing a using directive or an assembly reference?) WpfApplication1 ... 38

I think this could possibly be a reference issue, but I am not entirely sure. Thanks in advance!

Consider creating a window for your dialog and using it like this.

public class DialogWindow : Window
{
    // Your custom dialog window.
}

Call it like so:

      DialogWindow w = new DialogWindow();

      if(w.ShowDialog() != false)
      {
          // Do stuff here

      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM