开发者

Is it possible to associate same codebehind to multiple classes in silverlight like in flash

开发者 https://www.devze.com 2023-03-01 03:10 出处:网络
In flash one can point to a file on disk to associate the form with a class which name can be different from the form name so that you can multiple forms to the same class.

In flash one can point to a file on disk to associate the form with a class which name can be different from the form name so that you can multiple forms to the same class.

In Silverlight is it possible 开发者_Python百科somehow including by hacking vs studio project xml file by hand ?


It's performed using inheritence. You can define all you need in a base control, and derived controls use this code. For example you want define event handler that will be used for all your controls:

Define the event handler in the base class - 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 - defines new control inherited from theBaseControl. You just need to specify the base class

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>

Hope it what you have meant.

0

精彩评论

暂无评论...
验证码 换一张
取 消