I have wanted to be able to inherit my ASP.NET pages and controls from a generic base class a couple of times in the past. I can of course make the page derive from a generic base class in the code behind file, but then I end up with lots of code behind files whose only purpose in life it is to derive the page or control from that base class.
XAML makes this possible using the x:TypeArguments="..." attribute off the Window or Control element: the type(s) declared in TypeArguments will be used as generic type parameters for the class defined in the x:Class="..." attribute.
Up until now, I thought this was impossible in ASP.NET. Thanks to a post by Scott Guthrie (ASP.NET MVC Design Gallery and Upcoming View Improvements with the ASP.NET MVC Release Candidate) I now know better: the exact procedure is described in another post by Tim Barcz (Strongly-Typed ViewData Without A Codebehind):
<%@ Page Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage`1[[ABCCompany.MVC.Web.Models.LoginData, ABCCompany.MVC.Web]]" %>
A couple of caveats here:
- The "`1[[" does not start with a straight apostrophe, but rather a single left quote (the key to the left of the "1/!" key on a querty keyboard).
- Don't forgot to delete the designer-generated code file, and probably the code behind file too (it is just empty, isn't it?)
- ReSharper doesn't like this notation