如何创建多个用户控件指向一个code隐藏文件在Silverlight 4 [英] how to create Multiple user control that pointing single code behind file in silverlight 4

查看:196
本文介绍了如何创建多个用户控件指向一个code隐藏文件在Silverlight 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序,其中我有2个用户控制, 是有可能的是,我们有2个XAML用户控件的页面,并有1 code背后xaml.cs文件?

I am creating a application, in which i have 2 user control, is it possible that, we have 2 xaml user control page and having 1 code behind xaml.cs file?

推荐答案

创建三个文件刚开始时,先在code隐藏cs文件是一个简单的类创建: -

Start off by creating three files, first the "code-behind" .cs file is created as a simple class:-

 public class MyCommonUserControl : UserControl
 {

 }

请注意它有没有的InitializeComponent 电话。

现在创建一个新的用户控件然后修改它的XAML看起来像这样: -

Now create a new UserControl then modify its xaml to look like this:-

<local:MyCommonUserControl x:Class="YourApp.FirstMyCommonUserControl "
    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:YourApp"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>

</local:MyCommonUserControl >

请注意添加了的xmlns:本地别名指向您的应用程序的命名空间则用户控件标签的变化到基本控制,我们真正想要的。

Note the addition the xmlns:local alias to point to your app's namespace then the change of the UserControl tag to the base control we actually want.

您应该修改.xaml.cs这样: -

You would modify the .xaml.cs to this:-

public partial class FirstMyCommonUserControl : MyCommonUserControl 
{
    public FirstMyCommonUserControl()
    {
        InitializeComponent();
    }
}

这是所有.xaml.cs需要遏制。

That is all the .xaml.cs needs to contain.

您可以再重复此为 SecondMyCommonUserControl 等。将所有的公用code在基 MyCommonUserControl 类。

You can then repeat this for SecondMyCommonUserControl and so on. Place all the common code in the base MyCommonUserControl class.

它是一种可惜MS没想到这个摆在首位,添加一个空的虚拟InitializeComponent方法底层用户控件和具有.gics自动生成的$ C $ç覆盖的方法将意味着我们可以免除在这些情况下,这superflous .xaml.cs文件。

Its a pity MS didn't anticipate this in the first place, adding an empty virtual InitializeComponent method to the underlying UserControl and having the .g.i.cs auto-generated code override the method would have meant that we could dispense with this superflous .xaml.cs file in these cases.

这篇关于如何创建多个用户控件指向一个code隐藏文件在Silverlight 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆