在silverlight 4创建指向文件后面的单个代码的多用户控件
问题说明
我正在创建一个应用程序,其中我有 2 个用户控件,是否有可能,我们有 2 个 xaml 用户控制页面并且在 xaml.cs 文件后面有 1 个代码?
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?
正确答案
首先创建三个文件,首先将代码隐藏".cs 文件创建为一个简单的类:-
Start off by creating three files, first the "code-behind" .cs file is created as a simple class:-
public class MyCommonUserControl : UserControl
{
}
注意它没有 InitializeComponent
调用.
现在创建一个新的 UserControl
然后修改它的 xaml 看起来像这样:-
Now create a new UserControl
then modify its xaml to look like this:-
<local:MyCommonUserControl x:
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:local
别名以指向您应用的命名空间,然后将 UserControl
标记更改为我们真正想要的基本控件.
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
重复此操作,依此类推.将所有通用代码放在 MyCommonUserControl
基类中.
You can then repeat this for SecondMyCommonUserControl
and so on. Place all the common code in the base MyCommonUserControl
class.
遗憾的是,MS 一开始并没有预料到这一点,在底层 UserControl
中添加了一个空的虚拟 InitializeComponent 方法,并让 .gics 自动生成代码 override
该方法意味着在这些情况下我们可以省去这个多余的 .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.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangcbjfj
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20