UWP:InAppNotification 未显示

UWP: InAppNotification not showing up

我正在尝试使用 Windows Community Toolkit Sample App 中的 InAppNotification 控件,代码如下

<Grid>
    <tk_ctl:InAppNotification
            x:Name="InAppNotification"
            ShowDismissButton="True"
            Width="252.5"
            Content="In App Notification example"
            StackMode="Replace"/>
</Grid>

但是,当我 运行 应用程序时,我没有看到任何类型的弹出窗口。我错过了什么吗?也可以自定义 InAppNotiifcation,使关闭按钮位于顶部吗?

您需要调用 InAppNotification.Show 方法来显示您的通知。

如果您想要自定义 InAppNotification,您可以使用覆盖 InAppNotification.Show(DataTemplate, Int32) 指定 DataTemplate 作为通知的内容。

您可以检查以下代码作为示例:

MainPage.xaml:

<Page.Resources>
    <DataTemplate x:Key="InAppNotificationWithButtonsTemplate">
        <UserControl>
            <Grid>
                <Grid x:Name="RootGrid" >
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <StackPanel x:Name="ButtonsStackPanel" Grid.ColumnSpan="2" Grid.Row="0" 
                        Orientation="Horizontal" VerticalAlignment="Center">
                        <Button x:Name="YesButton" Content="Confirm" Width="100" Height="32"  />
                        <Button x:Name="NoButton" Content="Dismiss" Width="100" Height="32" Margin="10 0 0 0"  />
                    </StackPanel>

                    <TextBlock x:Name="TextBlock" Grid.ColumnSpan="2" Grid.Row="1"
                        Text="Do you Confirm it?" VerticalAlignment="Center" />

                </Grid>
            </Grid>
        </UserControl>
    </DataTemplate>
</Page.Resources>
<Grid>
    <tk_ctl:InAppNotification x:Name="InAppNotification" ShowDismissButton="True"
                      Content="In App Notification example"
                              VerticalOffset="100"
                              HorizontalOffset="0"
                      StackMode="Replace">
    </tk_ctl:InAppNotification>

</Grid>

MainPage.xaml.cs:

……
object inAppNotificationWithButtonsTemplate;
bool isTemplatePresent = Resources.TryGetValue("InAppNotificationWithButtonsTemplate", out inAppNotificationWithButtonsTemplate);
if(isTemplatePresent&&inAppNotificationWithButtonsTemplate is DataTemplate)
{
    InAppNotification.Show(inAppNotificationWithButtonsTemplate as DataTemplate);
}