Http://www.cnblogs.com/024hi/archive/2010/05/24/1743110.html
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 a way to use the background to spell the XAML (you can find it on the Internet ).ProgramPersonnel, these solutions are not very rational.
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.
The following is an example (CodeThe prototype of the tank war that I created on the 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 :)
Author: Purple eternity
Source: http://024hi.cnblogs.com/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page; otherwise, the legal liability is retained.
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 have worked tirelessly have found the way to use the background to spell the XAML (you can find it on the Internet). These solutions are not very rational 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 :)