WP8.1使用用户控件

时间:2015-12-11 21:10:33

标签: c# silverlight user-controls windows-phone-8.1

我想创建一个包含多个标签内容的小应用。现在可以使用枢轴或全景图轻松完成此操作,只需轻扫选项卡即可。但是我更喜欢使用用户控件来做到这一点。现在我创建了一个空白的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
        }

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

1 个答案:

答案 0 :(得分:0)

这并不困难。我只是添加了一个stackpanel,并在tap事件中创建了用户控件并将其存储在内存中,然后在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"/>