WP8.1使用用户控件

WP8.1 use user control

我想创建一个包含多个标签内容的小应用程序。现在,这可以通过枢轴或全景图轻松完成,只需轻扫选项卡即可。但是我更喜欢使用用户控件来做到这一点。现在我已经创建了一个空白的 silverlight 应用程序和一个带有 2 个文本框和 1 个按钮的用户控件。 在我的主页上,我有一个按钮,它应该弹出并使用 Tap 事件设置用户控件。 这是按钮

的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>

用户控制

<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>

这里是点击事件

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

如何切换到用户控制?我该如何使用它?

这并不难。我只是添加了一个堆栈面板和点击事件,我刚刚创建了用户控件并将其存储在内存中,然后在堆栈面板中添加了一个子项。

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

堆栈面板

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