12 Replies - 1392 Views - Last Post: 26 August 2019 - 03:11 AM Rate Topic: -----

#1 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 22-February 17

What does x:Class really mean in WPF?

Posted 19 August 2019 - 03:06 PM

I found this on the x:Class attribute:

x:Class : Specifies the CLR namespace and class name for the class that provides code-behind for a XAML page. You must have such a class to support code-behind per the WPF programming model, and therefore you almost always see x: mapped, even if there are no resources.

Ok, I think I get that.

Let's say I have 3 folders based on the MVVM stucture, viz. Models, ViewModels, and Views. In views I create a new window called Windows1. So in the Solution explorer I see Windows1.xaml created with a sub file called Window1.xaml.cs.

If I open Windows1.xaml the 1st line says:

<Window x:Class="WPFUI.Views.Window1"
xmlns="http://...

My project name is WPFUI, the folder is Views, so what does the Window1 refer to? The Windows1.xqml.cs subfile?

What if I wanted to use a different code-behind file? Could I create it in the Models folder, call it MyOwnFile.cs and update to:

<Window x:Class="WPFUI.ViewModels.MyOwnFile"
xmlns="http://...

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: What does x:Class really mean in WPF?

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,975
  • Joined: 05-May 12

Re: What does x:Class really mean in WPF?

Posted 19 August 2019 - 03:48 PM

It's not really the folder that matters, but the fully qualified name of the type that matters. Your MyOwnFile class should be in the ViewModels namespace, and ViewModels namespace needs to be in the WPFUI namespace. Something link this:
namespace WPFUI
{
    namespace ViewModels
    {
        partial class MyOwnFile : Window
        {
            :
        } 
    }
}


OR
namespace WPFUI.ViewModels
{
    partial class MyOwnFile : Window
    {
        :
    } 
}



In .NET Framework 2.0 and 3.5, Microsoft eschewed the Java convention of folders mapping to namespaces, but it looked like for WPF and MVC projects in .NET Framework 4.0 Microsoft started following suit where the folders hints at what namespace files within that folder belong to. You are still free to override that convention in C#. (If you tried to go against that convention in the Java world, I suspect that people will come at you as if you had been accused of witchcraft by the Spanish Inquisition.)
Was This Post Helpful? 1
  • +
  • -

#3 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 22-February 17

Re: What does x:Class really mean in WPF?

Posted 19 August 2019 - 05:50 PM

Ok, so I ran another little test.

I created a new window under Views called Testwindow.This lead to two files being created under Views, viz.

Testwindow.xaml and Testwindow.xaml.cs.

1. Is my understanding correct that Testwindow.xaml.cs is the default back-end code file for Testwindow.xaml?

Then, to make sure I understood what you said, I created a file called ShellViewModel.cs in the ViewModels folder (also under WPFUI project).

The 1st line of Testwindow.xaml file I changed from:

<Window x:Class="WPFUI.Views.TestWindow"

to

<Window x:Class="WPFUI.ViewModels.ShellViewModel"

Only problem I encountered was the following in the Testwindow.xaml.cs file:



namespace WPFUI.Views
{
    /// <summary>
    /// Interaction logic for Testwindow.xaml
    /// </summary>
    public partial class TestWindow : Window
    {
        public TestWindow()
        {
           InitializeComponent();
        }
    }
}



All else was fine except it did not like InitializeComponent() and gave error message:

The name InitializeComponent does not exist in the current context.

I comment it out and all is well. Why?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 22-February 17

Re: What does x:Class really mean in WPF?

Posted 19 August 2019 - 06:44 PM

Thinking about it, InitialiseComponent() is a constructor and once it is not referenced in the Testwindow.xaml file the error occurs cause it has nothing to construct. Or maybe I have it all wrong?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,975
  • Joined: 05-May 12

Re: What does x:Class really mean in WPF?

Posted 19 August 2019 - 06:47 PM

That is because of your use of Visual Studio trying to do some auto generation magic behind your back, and that magic not happening because you broke the "convention" that it uses.

Note the keyword partial. The reason why that is present is because Visual Studio also generates another code that implements the rest of the custom Window code into a partial class named ShellViewModel. That autogenerated code includes the InitializeComponent() method, declarations for all elements for which you provide an "x:Name" attribute for, as well as any event handlers that you may hook up. That autogenerated stuff should now go into "ShellViewModel.autogen.cs" (the name maybe wrong -- I am away from a PC right now to actually look.) Since you are still trying to compile a class named TestWindow in that last chunk of code there, that partial class will not merge with the ShellViewModel partial class.

To do WPF, you actually don't need all the autogenerated stuff that VS provides for you (but it sure gets rid of all the drudge work). You could hand code your WPF just like hand coding WinForms. Most people choose not to and just let the magic happen, just like the way most people let the magic of fuel injection and automatic transmission happen. Sometimes, though, it helps to understand how the magic happens.
Was This Post Helpful? 1
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,975
  • Joined: 05-May 12

Re: What does x:Class really mean in WPF?

Posted 19 August 2019 - 08:44 PM

Now that I'm back at a PC. Some minor correction to the post above.

First, the generated files are named "Testwindow.g.cs" and "Testwindow.g.i.cs". (The contents look to be the same. I don't recall right now why two identical files are generated.) Anyway the partial class name within is still TestWindow.

After you change the X:Class attribute to "ShellViewWindow", the same files are generated, but if you look inside, the partial class is now ShellViewWindow.

To discover the generated files, look in the obj directory of your project after you build it. You'll find all the intermediate files used by Visual Studio.
Was This Post Helpful? 1
  • +
  • -

#7 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 22-February 17

Re: What does x:Class really mean in WPF?

Posted 20 August 2019 - 03:44 PM

Skydiver, I'll have to think and play around some more on this cause it is really confusing...

But as Murphy said,"The light at the end of the tunnel is the headlamp of an oncoming train.!"

O'Toole's commentary, "Murphy was an optimist!"
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,228
  • Joined: 12-December 12

Re: What does x:Class really mean in WPF?

Posted 21 August 2019 - 07:42 AM

As someone once said, "I can see the carrot at the end of the tunnel".
Was This Post Helpful? 0
  • +
  • -

#9 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 22-February 17

Re: What does x:Class really mean in WPF?

Posted 25 August 2019 - 08:06 PM

View Postandrewsw, on 21 August 2019 - 07:42 AM, said:

As someone once said, "I can see the carrot at the end of the tunnel".


It's a modern train and the LED headlamp is mistaken for a carrot!

Quote

To discover the generated files, look in the obj directory of your project after you build it. You'll find all the intermediate files used by Visual Studio.


What is the name of this object directory please? Could not find them.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,975
  • Joined: 05-May 12

Re: What does x:Class really mean in WPF?

Posted 25 August 2019 - 08:22 PM

The objects directory is named "obj".
Was This Post Helpful? 0
  • +
  • -

#11 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 22-February 17

Re: What does x:Class really mean in WPF?

Posted 25 August 2019 - 09:50 PM

Ok, got you. I was looking in the Solution Explorer of Visual Studio! Found them when I went searched in Windows Explorer.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#12 valkenberg   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 22-February 17

Re: What does x:Class really mean in WPF?

Posted 26 August 2019 - 01:33 AM

One last question - in Testwindow.xaml.cs why is Testwindow : Screen a partial class? Is this because this class is meant to be augmented with code in a file in TestwindowViewModel.cs which is in the ViewModel directory as per the MVVM format? Recall that I have WPFUI.Views.Testwindow.xaml and below that is WPFUI.Views.Testwindow.xaml.Testwindow.xaml.cs.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,228
  • Joined: 12-December 12

Re: What does x:Class really mean in WPF?

Posted 26 August 2019 - 03:11 AM

Note that there is a button in Solution Explorer to show all files, to display the obj folder.

I am not at my computer but a number of window or screen classes are partial because there is an amount of designer code generated for us that does a lot of wiring up. The show all files button will reveal these files which we should not alter.

I suspect that it should be possible, using a plain text editor, to create a WPF application from scratch, but we would end up writing a lot of template code that VS does for us. It would still be a fruitful exercise to attempt this, to understand the inner workings.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1