Silverlight technology yinqiao 6-inheritance of usercontrol

Source: Internet
Author: User

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

  •  

    •  

        xmlns:my="clr-namespace:EternalTank"

    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?

    • Summary

    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 :)

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.