简体   繁体   English

WP8.1使用用户控件

[英]WP8.1 use user control

I want to create a small app with multiple tab contents. 我想创建一个具有多个标签内容的小型应用程序。 Now this could be easily done with a pivot or a panorama and simply swipe through the tabs. 现在,只需使用枢轴或全景图即可轻松完成此操作,只需在选项卡上滑动即可。 However I prefer using user controls to do that. 但是我更喜欢使用用户控件来做到这一点。 Now I have created a blank silverlight app and a user control with 2 textboxes and 1 button. 现在,我创建了一个空白的Silverlight应用程序和一个带有2个文本框和1个按钮的用户控件。 On my mainpage I have a button which should bring up and set the user control with a Tap event. 在我的主页上,我有一个按钮,该按钮应调出并用Tap事件设置用户控件。 Here is the xaml of the button 这是按钮的xaml

<phone:PhoneApplicationPage
x:Class="set.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 is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="157,49,0,0" VerticalAlignment="Top" Tap="button_Tap"/>

    </Grid>
</Grid>

User control 用户控制

<UserControl x:Class="set.Helper.Login"
    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}" Margin="0,0,10,10">
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="55" TextWrapping="Wrap" Text="email" VerticalAlignment="Top" Width="333" Margin="74,97,0,0" FontSize="16" FontFamily="Arial" FontStyle="Italic" GotFocus="textBox_GotFocus" Foreground="#FF767575"/>
        <TextBox x:Name="textBox_Copy" HorizontalAlignment="Left" Height="55" TextWrapping="Wrap" Text="password" VerticalAlignment="Top" Width="333" Margin="74,157,0,0" FontSize="16" FontFamily="Arial" FontStyle="Italic" GotFocus="textBox_Copy_GotFocus" Foreground="#FF767575"/>
        <Button x:Name="button" Content="Login" HorizontalAlignment="Left" Margin="74,217,0,0" VerticalAlignment="Top" Height="71" Width="333" Foreground="White" Background="#FF002DA6"/>

    </Grid>
</UserControl>

and here is the tap event 这是点击事件

private void button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //set user interface here
        }

How can I switch to user control? 如何切换到用户控制? How can I use it? 如何使用?

This wasn't so difficult. 这不是那么困难。 I simply added a stackpanel and on tap event I just created the user control and stored it in memory and then added a children in the stackpanel. 我只是添加了一个堆栈面板,而在点击事件中,我只是创建了用户控件并将其存储在内存中,然后在堆栈面板中添加了一个子控件。

private void button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Helper.Login loging = new Helper.Login();
            stkTest.Children.Add(loging);
        }

stackpannel 堆栈面板

<StackPanel Name="stkTest" HorizontalAlignment="Left" Height="608" Margin="10,150,0,0" Grid.RowSpan="2" VerticalAlignment="Top" Width="460"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM