Silverlight:页面/控件继承的二种写法
前言:此乃水文,高手绕道.
只要是稍微象样点的项目,一般都会先弄一个PageBase或UserControlBase做基类,然后其它页面/用户控件继承自它,标准的写法如下:
先定义基类: PageBase.cs
using System.Windows.Controls;
namespace sl_test
{
    public class PageBase:UserControl
    {
    }
}
然后创建一个用户控件Page1继承自它:
后端cs部分:
namespace sl_test
{
    public partial class Page1 : PageBase
    {
        public Page1()
        {
            InitializeComponent();
        }
    }
}
对于silverlight而言,光这样是不够的,前端xaml部分也要修改:
<local:PageBase x:Class="sl_test.Page1"
    xmlns:local="clr-namespace:sl_test"
    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">
    </Grid>
</local:PageBase>
即xaml要继承一个类,得同时改xaml.cs与xaml文件二处。
今天又看到了另一种偷懒的做法,可以只修改Xaml即可,方法就是直接去掉xaml.cs文件中的:PageBase,即将:
public partial class Page1 :PageBase
变成:
public partial class Page1
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号