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. []: secureCRT连接ubuntu问题- The remote system refused the connection

    secureCRT连接ubuntu问题- The remote system refused the connection http://jxyang.iteye.com/blog/1484915 解 ...

  2. 【linux驱动】linux驱动总览

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

  3. openstack ovs-gre 网速慢解决方案

    Hint: if you want to check if this answer will solve your issue, execute ifconfig eth0 mtu 1400 on t ...

  4. 基于visual Studio2013解决C语言竞赛题之1023判断排序

         题目 解决代码及点评 /* 23. 有10个两位整数,把这些数作以下变化,如果它是素数, 则把它乘以2,若它是偶数则除以2,其余的数减1, 请将变化后的10个数按从小到大 ...

  5. 配置开源项目 SlidingMenu 的问题

    最近想研究一下开源项目 SlidingMenu,单是配置项目就花了好长的时间,断断续续的尝试,终于配置成功了,写下来和大家分享一下经验. Step 1:导入依赖的项目和例子 打开项目 File -&g ...

  6. 在SharePoint 2013 中使用文档库Scheduling (计划公布功能)

    本文讲述在SharePoint2013 中使用文档库Scheduling (计划公布功能)的步骤和注意的事项. 文档库Scheduling (计划公布功能) 用于设定当文档通过审批后特定的时间区间内才 ...

  7. 执行shell脚本提示“syntax error near unexpected token for((i=0;i&lt;$length;i++))”

    sh脚本例如以下: #!/usr/bin/env bash county="3 4 5 6 7 8 9 10 11 12 16 29 39 44 53 62 72 84 97 115 128 ...

  8. IMAP和POP3有什么差别?

    servCode=6010376">POP3协议同意电子邮件client下载server上的邮件,可是在client的操作(如移动邮件.标记已读等),不会反馈到server上.比方通过 ...

  9. 第12届北师大校赛热身赛第二场 B起床的烦恼

    题目链接:http://www.bnuoj.com/bnuoj/contest_show.php? cid=3570#problem/43572 题目大意: Nono从一開始数数,他每数一个数时会计算 ...

  10. EXT2/EXT3文件系统(二)

         整理自<鸟哥的Linux私房菜>,整理者:华科小涛http://www.cnblogs.com/hust-ghtao/ 接EXT2/EXT3文件系统(一):   2.3 Supe ...