In Silverlight, inheritance of usercontrol is always a hassle. inheriting a view (usercontrol, page, and other base classes) is more troublesome than ASP. NET. There are various problems in defining inheritance in the usercontrol class, for example, to modify. g files. Even those who work tirelessly have found the way to use the back-end XAML (you can find it on the Internet). These solutions are not very ideal for programmers with cleanliness.
In fact, from sl3, Ms has already told us how to inherit usercontrol correctly. As long as you create a page (note, not usercontrol), you can see the clues.
Let me give you an example (the code is taken from the prototype of the tank war that I created on a whim)
- First, we have a base class called baseobject.
Using system. windows; using system. windows. controls; namespace eternaltank {public class baseobject: usercontrol {double _ x; Public Double X {get {_ x = canvas. getleft (this); Return _ x;} set {canvas. setleft (this, value); _ x = value ;}} double _ y; Public Double Y {get {_ y = canvas. gettop (this); Return _ y;} set {canvas. settop (this, value); _ y = value ;}} public objectdirection ction {Get; Set ;} /// <summary> // tank speed // </Summary> Public double speed {get {return (double) getvalue (speedproperty );} set {setvalue (speedproperty, value) ;}} public static readonly dependencyproperty speedproperty = dependencyproperty. register ("Speed", typeof (double), typeof (baseobject), new propertymetadata (10d ));}}
The significance of the code here is not important, but remember that it is a base class and inherits usercontrol, so that other usercontrol can inherit it.
- Next is a class derived from it.
namespace EternalTank.Assets { public partial class Brick : BaseObject { public Brick() { InitializeComponent(); } }}
The brick of this column inherits baseobject. The following is the most critical step.
- In the brick XAML, we define the following:
<my:BaseObject x:Class="EternalTank.Assets.Brick" xmlns:my="clr-namespace:EternalTank" 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:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <Rectangle Fill="Red" Width="13" Height="13"></Rectangle> </Grid></my:BaseObject>
Is it different from general usercontrol?
1. First declare the namespace in XAML. For this example
2. Then let it be the heel element of usercontrol.
<my:BaseObject>
</my:BaseObject>
Run it to see if there is no problem at all?
As mentioned in the beginning, this method has been widely used in Silverlight. For example, in navigation: page, it is only worth the attention.
I personally think we should avoid this inheritance if it is not necessary. After all, it also increases our workload and difficulty in understanding code. My advice is to use mvvm flexibly. Isn't there a common base class for our viewmodel :)