Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which file do I write the code in? There are many .xaml.cs files

Tags:

c#

wpf

xaml

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.

like image 386
QueenSaphos Avatar asked Feb 13 '23 08:02

QueenSaphos


1 Answers

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();
            }
        }
    }
    
like image 119
Sumner Evans Avatar answered Feb 16 '23 02:02

Sumner Evans