WP基于代理编程与其说是一个语法点不如说是一种设计思想,它通过代理(委托)实现控制类与UI(或者其他类)的交互。
下面这个例子很简单,点击按钮生成随机数。
<phone:PhoneApplicationPage
x:Class="DeleTask.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">
<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- 本地化说明:
若要本地化显示的字符串,请将其值复制到应用程序的非特定语言资源文件(AppResources.resx)
中的适当命名的键,然后
将属性的引号之间的硬编码文本值
替换为其路径指向该字符串名称的绑定子句。
例如:
Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
此绑定指向模板的名为“ApplicationTitle”的字符串资源。
在“项目属性”选项卡中添加受支持的语言将会为
每种语言创建一个新的 resx 文件,该文件可以包含 UI 字符串的翻译值
。这些示例中的绑定将导致在运行时从
与应用程序的 CurrentUICulture 匹配的 .resx 文件中
提取属性的值。
-->
<!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
</StackPanel>
<Button x:Name="Bt" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="149,74,0,0" Grid.Row="1" Click="Bt_Click"/>
<TextBox x:Name="Tb" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="456" Margin="0,173,0,0" Grid.Row="1"/>
<!--ContentPanel - 在此处放置其他内容-->
<!--取消注释,以显示对齐网格,从而帮助确保
控件在公用边界上对齐。图像在系统栏中显示时的
上边距为 -32px。如果隐藏了系统栏,则将此值设为 0
(或完全删除边距)。
在发送之前删除此 XAML 和图像本身。-->
<!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" Margin="0,-32,0,0" Grid.Row="0" Grid.RowSpan="2" IsHitTestVisible="False" />-->
</Grid>
</phone:PhoneApplicationPage>
背后的CS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using DeleTask.Resources;
namespace DeleTask
{
public partial class MainPage : PhoneApplicationPage
{
public NumberTask task;
// 构造函数
public MainPage()
{
InitializeComponent();
}
private void Bt_Click(object sender, RoutedEventArgs e)
{
task = new NumberTask();
DeleTask.NumberTask.SetTb set = new NumberTask.SetTb(setNum);
task.myDele = set;
task.setNum();
}
private void setNum(int n)
{
Tb.Text = n.ToString();
}
}
}
控制类(生成随机数):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeleTask
{
public class NumberTask
{
public delegate void SetTb(int n);
public SetTb myDele;
public void setNum()
{
Random r = new Random();
int x = r.Next(1000);
myDele(x);
}
}
}
上面2个类通过代理来实现控制权的转移和沟通。