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. Windows的TCP协议参数

    注册表编辑器:regedit 表项:HKEY_LOCAL_MACHINE\SYSTEM\CurentControlSet\Services\Tcpip\Parameters 窗口扩大因子 & ...

  2. C# char[]与string之间的相互转换

    string 兑换 Char[] string ss = "abcdefg"; char[] cc = ss.ToCharArray(); Char[] 转换成string str ...

  3. AdTime:多屏时代下传统媒体的鼓起

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzk1MTQzNQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. ASP.NET Core Authorization

    ASP.NET Core Authorization 本文目录 Asp.net Core 对于授权的改动很友好,非常的灵活,本文以MVC为主,当然如果说webapi或者其他的分布式解决方案授权,也容易 ...

  5. Docker学习笔记(1) — docker 常用命令

    1. docker version显示 Docker 版本信息.2. docker info显示 Docker 系统信息,包括镜像和容器数.3. docker searchdocker search ...

  6. ctfmon.exe开机无法自己主动启动

    打开命令提示符(開始菜单--执行--输入:cmd),输入下面命令(复制粘贴就可以): reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Ru ...

  7. git clone cm source &amp; cm vs android version

    $ git clone https://github.com/CyanogenMod/android_packages_apps_DeskClock.git -b cm-9.0.0 CM4代表(And ...

  8. google浙大招聘笔试题 师兄只能帮你到这儿了

    google浙大招聘笔试题 一.单选1.80x86中,十进制数-3用16位二进制数表示为?00100002.假定符号-.*.$分别代表减法.乘法和指数运算,且 1)三个运算符优先级顺序是:-最高,*其 ...

  9. FindChildControl与FindComponent

    前两天编码遇到了要使用FindChildControl方法获取指定名称的TSpeedButton按钮,结果折腾了半天就是没得结果(基础不扎实,呵呵),于是赶紧搜索了下,补习关于这两个方法的用法. TW ...

  10. hbase:应用开发

    开发环境: hadoop: hadoop-1.1.2 hbase: hbase-0.94.11-security eclipse:Juno Service Release 2 配置Eclipse 通过 ...