UserControl的使用

开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需
关心内部逻辑,只需获取处理后的结果即可

创建UserControl步骤如下:
1.创建xaml布局,UserControl外观,创建UIs
2.在对应.cs文件中添加内部业务逻辑代码

ex:
创建一个带TextBlock的Button

1.xaml布局文件TextBlockButton.xaml

<UserControl x:Class="UserControlTest.UserControls.TextBlockButton"
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"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"> <Grid x:Name="LayoutRoot"
Background="{StaticResource PhoneChromeBrush}">
<StackPanel>
<TextBlock Name="textBlock"
HorizontalAlignment="Center"
Text="textBlock"/>
<Button Name="btn"
Content="Button"/>
</StackPanel>
</Grid>
</UserControl>

TextBlockButton

2..cs代码

    public partial class TextBlockButton : UserControl
{
// 步长
public int Step { get; set; } public TextBlockButton()
{
InitializeComponent(); this.btn.Click += btn_Click;
} void btn_Click(object sender, RoutedEventArgs e)
{
this.textBlock.Text = (Step++) + "";
}
}

TextBlockButton.xaml.cs

3.使用UserControl
  a).引入UserControl

  b).获得引用

在需要使用的页面添加引用

<phone:PhoneApplicationPage
x:Class="UserControlTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True" xmlns:userControls="clr-namespace:UserControlTest.UserControls"
> <!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions> <!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel> <!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<userControls:TextBlockButton Name="textBlockBtn"
Step="0"/>
</StackPanel>
</Grid>
</Grid> </phone:PhoneApplicationPage>

MainPage.xaml

在MainPage.xaml.cs文件中通过name属性可以引用UserControl

[Windows Phone学习笔记]UserControl的使用的更多相关文章

  1. Windows Security 学习笔记

    对于Windows 在 Security 方面的学习. 纯兴趣. UNIX 的另外开一条路线学习. 话说今天查gpedit.msc的资料的时候发现 M$ 官网上怎么连个文档都没有. 后来才点了 gpe ...

  2. Windows程序设计学习笔记(一)Windows内存管理初步

    学习Windows程序设计也有一些时间了,为了记录自己的学习成果,以便以后查看,我希望自己能够坚持写下一系列的学习心得,对自己学习的内容进行总结,同时与大家交流.因为刚学习所以可能有的地方写不不正确, ...

  3. Windows Internals学习笔记(八)IO系统

    参考资料: 1. <Windows Internals> 知识点: ● 当一个进

  4. Windows Internals学习笔记(七)Image Loader

    参考资料: 1. <Windows Internals> 2. Fibers 知识点: ● 当一个进程在系统上启动时,内核将创建一个进程对象来代表它,并执行各种内核相关的初始化任务.然而, ...

  5. Windows Internals学习笔记(六)Windows关键系统组件

    参考资料: 1. <Windows Internals> 2. Dependency Walker 3. Ntoskrnl.exe 4. Livekd的使用 5. WinDbg的使用(一) ...

  6. Windows调试学习笔记:(二)WinDBG调试.NET程序示例

    好不容易把环境打好了,一定要试试牛刀.我创建了一个极其简单的程序(如下).让我们期待会有好的结果吧,阿门! using System; using System.Collections.Generic ...

  7. Windows调试学习笔记:(一)WinDBG中加载SOS和CLR

    最近产品环境出现了部分服务器当机情况,虽然重启之后问题就不出现了.但本着彻底解决问题的态度,想要找到root cause.多次尝试Visual Studio失败(可能是代码惊醒了优化和签名)之后,决定 ...

  8. Windows Internals学习笔记(五)Synchronization

    参考资料: 1. <Windows Internals> 2. 自旋锁spinlock剖析与改进 3. Lock指令前缀 4. Lock指令前缀(二) 5. Kernel Dispatch ...

  9. Windows Internals学习笔记(四)Trap Dispatching

    参考资料: 1. <Windows Internals> 知识点: ● 陷阱trap:它是一种处理器机制,用以在某一异常或中断出现时,捕捉该执行线程,并将其控制权转交到操作系统中某一固定位 ...

随机推荐

  1. SQL之单行函数

    单行函数语法: function name(column|expression,[arg1,arg2,...]) 参数说明: function name:函数名称 column:数据库列名 expre ...

  2. XLSTransformer生成excel文件简单演示样例

    项目结构图: 项目中所用到的jar,能够到http://www.findjar.com/index.x下载 ExcelUtil类源代码: package util; import java.io.IO ...

  3. PHP - 遍历文件夹下的所有文件名

    /** * * 函数名:myreaddir($dir) * 作用:读取目录所有的文件名 * 参数:$dir 目录地址 * 返回值:文件名数组 * * */ function myreaddir($di ...

  4. 【linux驱动笔记】字符设备驱动相关数据结构与算法

    欢迎转载,转载时需保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...

  5. Android中Parcelable序列化总结

    在使用Parcelable对android中数据的序列化操作还是比较有用的,有人做过通过对比Serializable和Parcelable在android中序列化操作对象的速度比对,大概Parcela ...

  6. net core与golang web

    Asp.net core与golang web简单对比测试 最近因为工作需要接触了go语言,又恰好asp.net core发布RC2,就想简单做个对比测试. 下面是测试环境: CPU:E3-1230 ...

  7. QCompleter自动补全

     知识永远是那么多,想到什么就总结什么,今天记录一下关于我对QComplete的一些小见解.   官方文档中叙述如下:     可以在任何Qt的窗口小部件中使用QCompleter提供自动补全功能,如 ...

  8. windows 2003 域控制器(AD)的常规命令行操作以及修复

    查询服务器的角色 Netdom query fsmo 强制升级操作主机角色(如果两台DC都无损,可以直接用图形模式传送,这里指的是一台DC出问题,另一台强制升级占用角色的情况) Ntdsutil Ro ...

  9. cocos2d学习笔录1

    CCDirector的主要作用: 1.访问和改变场景: 2.访问cocos2d-x的配置细节 3.访问视图(OPENGL,UIVIEW,UIWINDOW): 4.暂停,恢复和结束游戏: 5.在UIKi ...

  10. 用angularjs开发下一代web应用(二):angularjs应用骨架(二)

    1.浅谈非入侵式JavaScript <div ng-click="doSomething()">...</div>这些指令和原来的事件处理器有下面不同之处 ...