如何使自定义弹出对话可重用

时间:2017-12-15 17:22:15

标签: c# .net uwp

所以我在UWP中创建了一个自定义弹出对话框,使其可重用,我使用了用户控件

<UserControl
    x:Class="ContentDialogueBox.PopUpCustomDialogueUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ContentDialogueBox"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">


    <ContentDialog  x:Name="MyContentDialogCustom"
                    VerticalAlignment="Stretch"
                    HorizontalAlignment="Stretch"
                    Title="Lorem Ipsum"
                    PrimaryButtonText="OKxxx"

                    SecondaryButtonText="Canc"
                     Margin="0,0,-98,0" Width="1000" >
        <Grid Background="Wheat">


            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock Text="Name" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBox PlaceholderText="First Name" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="10"/>

            <TextBlock Text="Middle Name" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1"/>
            <TextBox PlaceholderText="Middle Name" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="10"/>

            <TextBlock Grid.Row="2" Text="Name" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBox PlaceholderText="First Name" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="10"/>



        </Grid>
    </ContentDialog>
</UserControl>

我能够在包含名为ButtonShowContentDialog4的按钮的另一个页面中引用用户控件,当我单击页面中的按钮时,我想要实现的是显示PopUp对话框。请问我该如何做到这一点。

private void ButtonShowContentDialog4_Click(object sender, RoutedEventArgs e)
{
      //show the PopUp here when this button is clicked
}

1 个答案:

答案 0 :(得分:0)

有几种方法可以实现这一目标。除了您可以制作自定义 ContentDialog 而不是将其放在用户控件中之外,您可以实现这样的目标:

PopUpCustomDialogueUserControl 类中定义一个显示弹出窗口的方法:

public async Task ShowMyDialog() => await MyContentDialogCustom.ShowAsync();

获得它后,在xaml中的 Page 中添加 PopUpCustomDialogueUserControl som(请记住已添加合适的命名空间( ContentDialogueBox )在下面作为 local )并添加名称

<local:PopUpCustomDialogueUserControl x:Name="MyCustomDialog"/>

然后你可以在代码中调用它的方法:

private async void ButtonShowContentDialog4_Click(object sender, RoutedEventArgs e)
{
    await MyCustomDialog.ShowMyDialog();
}