2011-04-27 60 views

回答

1

它使用继承来执行。您可以在基本控件中定义所有需要的内容,并且派生控件使用此代码。比如你要定义事件处理程序,将被用于所有控件:

定义在基类的事件处理程序 - BaseControl.xaml.cs

namespace SilverlightApp 
{ 
    public partial class BaseControl : UserControl 
    { 
     public BaseControl() 
     { 
      InitializeComponent(); 
     } 

     // The event handler that used by derived classes 
     protected void Button_Click(object sender, RoutedEventArgs e) 
     { 
      // your implementation here 
     } 
    } 
} 

BaseControl.xaml

<UserControl x:Class="SilverlightApp.BaseControl" 
    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"> 

    <!-- your implementation here if needed --> 
</UserControl> 

MyControl1.xaml.cs - 定义从BaseControl继承的新控件。你只需要指定基类

namespace SilverlightApp 
{ 
    public partial class MyControl1 : BaseControl 
    { 
     public MyControl1() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

MyControl1.xaml

<local:BaseControl x:Class="SilverlightApp.MyControl1" 
    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" 
    xmlns:local="clr-namespace:SilverlightApp" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
     <!-- button uses event handler from the base class --> 
     <Button Content="My button" Click="Button_Click" /> 
    </Grid> 
</local:BaseControl> 

希望它你是什么意思。

+0

非常感谢你会尝试。 – user310291 2011-04-27 20:52:42

相关问题