4

I'm trying to wrap my head around where the code executes from, as this is what I want to achieve:

I have a main window winMainWindow with a toolbar at the top. Each item in the toolbar File --> Add New Company will open a new window, which will ideally be self-contained, do some processing, and then be closed.

Every time I add a new window to the project (right-click on solution --> Add --> Window), it creates a .xaml and .xaml.cs file for the new window. Say I create a new window called SettingsWindow: I understand how to use the SettingsWindow.xaml file, that's self-explanatory.

However, with the .xaml.cs file that it creates for each window, what is supposed to go in there?? I can't seem to figure out when that code will be executed - I haven't been able to find the Main method yet, so there must be a reason that's hidden.

It would make sense to have the SettingsWindow methods in the SettingsWindow.xaml.cs file, but how to I get the program to "switch" to that .cs file when the SettingsWindow has been opened?

Lastly, if I wanted to write a method called OpenThisWindow, which I want to be able to use for all my windows, where would I write this method? I believe it should be a public method, but I don't know where to place the code.

Thank you.

1
  • for the related MyFile.xaml file in the file named MyFile.xaml.cs. Jan 8, 2019 at 14:32

1 Answer 1

8

This tutorial from MSDN explains that:

This file [that is, the *.xaml.cs file] is a code-behind file that contains code to handle the events declared in MainWindow.xaml. This file contains a partial class for the window defined in XAML.

Basically, when an event is fired from the GUI element defined in the *.xaml file (for example: page load, button click, etc.) this file handles the event.

To answer your questions:

  1. What is supposed to go there?

    Your handler code. It will look similar to this:

    public void Button_Click(object sender, RoutedEventArgs e)
    {
        // Put what you want to do on button click here.
    }
    

    To create these handlers, double click on the item you want to handle (VS will pick the default handler). Alternatively or go to the "Events" in the "Properties" window (the "Events" button is the one with the lightning bolt) and double click on whatever event you would like to handle.

  2. OpenThisWindow function

    I would name it something like OpenWindow. Here is what your function should look like:

    public static void OpenWindow (Window windowToOpen)
    {
        windowToOpen.Show();
    }
    

    When you call this function you will have to use something like this:

    OpenWindow(new WindowName());
    

    You can put this function in a new class (Ctrl + Shift + A => Class) and call it something like ApplicationHelper.cs. Make it a static class and add the OpenWindow function to it:

    using System.Windows;
    
    namespace WpfApplication1
    {
        static class ApplicationHelper
        {
            public static void OpenWindow(Window windowToOpen)
            {
                windowToOpen.Show();
            }
        }
    }
    
2
  • So much sense!! :) Thank you. One more thing - I need something in the event handler that opens a different window. I tried the following: AddNewCompany winAddNew = new AddNewCompany(); Do I really want to be running this line every time someone presses the button, or should it go somewhere else where it only executes once? Thank you! Apr 1, 2014 at 4:46
  • @QueenSaphos Good question. If I were developing an application in WPF, I would probably do that every time someone presses the button (I am not an expert on this by any means). I'm not sure if that's the optimal approach, however. I'm sorry I can't give you a definite answer, but you might want ask a question about that on a Stack Exchange site. Apr 1, 2014 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.