代码改变世界

[Windows Phone学习笔记]UserControl的使用

2014-04-11 23:44  hellenism  阅读(285)  评论(0编辑  收藏  举报

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