zoukankan      html  css  js  c++  java
  • [Windows Phone学习笔记]UserControl的使用

    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

    人生就是一局不能Again的DOTA
  • 相关阅读:
    [Luogu P3626] [APIO2009] 会议中心
    杭电 1869 六度分离 (求每两个节点间的距离)
    杭电 1874 畅通工程续 (求某节点到某节点的最短路径)
    最短路径模板
    杭电 2544 最短路径
    POJ 1287 Networking (最小生成树模板题)
    NYOJ 1875 畅通工程再续 (无节点间距离求最小生成树)
    POJ 2485 Highways (求最小生成树中最大的边)
    杭电 1233 还是畅通工程 (最小生成树)
    杭电 1863 畅通工程 (最小生成树)
  • 原文地址:https://www.cnblogs.com/hellenism/p/3659797.html
Copyright © 2011-2022 走看看