mwolfe02
mwolfe02

Reputation: 24227

Inheriting from Window in WPF MVVM

I'm trying to implement the following solution: https://stackoverflow.com/a/1486481/154439. In the author's solution he writes,

I created an abstract ApplicationWindowBase class that inherits from Window.

I am not sure what he means here. I tried changing my XAML from <Window ... to <ApplicationWindowBase .... No luck.

The designer-generated file for the window inherits from System.Windows.Window, but as that file is designer-generated, making changes to it does me no good. I've tried various other approaches with no success.

I am very new to both WPF and MVVM. Am I missing something obvious?

Upvotes: 0

Views: 543

Answers (2)

user1228
user1228

Reputation:

First, xaml or not, these are all classes. Therefore, anything inheriting from another class (i.e., Window), must do so as any other class.

namespace SomeNamespace
{
    public sealed class MyWindow : Window
    {
        public object SomeNewProperty {get;set;}
    }
}

xaml is just a different type of serialization with its own set of rules. But it's still serialized XML, and as such everything must be traceable back to its original type.

How xaml does this is through a facility in XML called "namespaces". They fit well with our understanding of namespaces as C# developers, as we must map an XML namespace to a namespace in our application so that the xaml serializer can match an XML element with a CLR type.

Xaml uses a couple different ways of matching these namespaces. One looks like a URL and is defined by an assembly attribute (e.g., "xmlns="http://microsoft.com/whatevs"). This works when the assembly is external to the one you are writing in. In your case, you'll have to use the other method of identifying your namespace. This specialized XML namespace is parsed by the xaml serializer in order to identify the namespace and containing assembly of your classes. It goes by the form "clr-namespace:SomeNamespace;assembly=SomeAssembly". You can omit the assembly bit if the type is in the current assembly.

To put it all together with the above example, you would create an instance of your new window like this:

<t:MyWindow 
    xmlns:t="clr-namespace:SomeNamespace" 
    xmlns="the standard microsoft namespace here"
    t:OmitOtherStuffBecauseThisIsAnExampleLol="true">

Upvotes: 1

TrueEddie
TrueEddie

Reputation: 2233

Code to go along with Kevin's comment:

Code Behind

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : ApplicationWindowBase
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

XAML

<myPrefix:ApplicationWindowBase x:Class="StackOverflow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myPrefix="clr-namespace:StackOverflow"
        Title="TestWindow" Height="300" Width="300">
    <Grid>

    </Grid>
</myPrefix:ApplicationWindowBase>

Just make sure you use the correct namespace, mine happens to be StackOverflow.

Upvotes: 1

Related Questions