14

How can you specify a common base class in .xaml files for seperate Silverlight Page classes? I have a few common properties that I would like to share across pages, but I don't know how to do this without manually changing the base class in the .g.cs files each time they are generated.

Is this possible? I assume it is possible, since the ChildControl in the Toolkit, for example, derives from a different class. Am I overlooking something obvious?

Svante
  • 50,694
  • 11
  • 78
  • 122
tsimon
  • 8,362
  • 2
  • 30
  • 41

2 Answers2

35

All Silverlight "pages" are actually deriving from UserControl by default. So, here's what you need to do. Simple example, of course you'd probably want to declare Dependency properties, events, and more.

1. Create your class with the common properties

public class YourUserControlBase : UserControl
{
    public bool SomeProperty {get; set; }
}

2. Create/modify a Page's XAML

Add a XML namespace for the local assembly and namespace that contains your new base class, and remember you keep the x:Class attribute at the top of the file, but modify the UserControl root element to be the local name

Here's my updated file:

<local:YourUserControlBase
xmlns:local="clr-namespace:SilverlightApplication1"
x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

Modify the code-behind

(the Page.xaml.cs file, not the auto-generated one) to properly inherit from YourUserControlBase:

public partial class MainPage : YourUserControlBase
{
    public MainPage()
    {
        InitializeComponent();
    }
}

That should be it! Good luck.

Scott Solmer
  • 3,871
  • 6
  • 44
  • 72
Jeff Wilcox
  • 6,375
  • 1
  • 24
  • 31
  • Hey, thanks! I initially thought, 'interesting that he shares his name with the developer...' ;-). Love the autocomplete, too. I just built a custom templated control that made my bosses drool, so thanks. I took the credit, though =) – tsimon Aug 19 '09 at 10:19
  • Also, if you guys are out of features to add and want to add one that is more trouble than it's worth ... I was looking for something like: x:BaseClass="Namespace.YourUserControlBase" Thanks again! – tsimon Aug 19 '09 at 10:22
  • XAML compatibility is a tough one since its out there, but yeah I hear you on that one - I'll share the feedback. Glad to help! – Jeff Wilcox Aug 19 '09 at 15:21
  • 1
    Is there a way to do this with a base class that's generic? i.e. `public class ViewBase : UserControl where T : ViewModelBase {}` `public class MyView : ViewBase {}` – Toby Apr 13 '12 at 16:28
  • To make this more complete: can you provide the XAML for the base UC ? – Patrick Peters Aug 27 '12 at 08:39
  • @PatrickPeters AFAIK there is no XAML for the base class. The UI is only defined in the derived classes. – Tom Wilson Nov 03 '15 at 11:19
  • Thank you that (unnessecery) inheritance was the truly problem and i didn't figure it out ! – Mehdi Nourollah May 01 '19 at 13:27
6

Also: when your base class is in an other assembly (project) you can do this:

xmlns:Custom="clr-namespace:SilverlightApplication1;assembly=[Other.Assembly]"

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
Erik
  • 1,779
  • 1
  • 16
  • 8