【问题标题】:How to make a simple ContentDialog in .xaml?如何在 .xaml 中制作一个简单的 ContentDialog?
【发布时间】:2018-08-01 18:03:56
【问题描述】:

创建 ContentDialog 需要什么?

我基本上有一个按钮。当我点击该按钮时,ContentDialog 打开。 ContentDialog 应包含“这是一个示例”之类的文本和 2 个按钮。 <ConTentDialog></ContentDialog> 的外观如何?示例:

<Page
    x:Class="PDFViewerSDK_Win10.PDFReaderPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PDFViewerSDK_Win10"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" SizeChanged="OnSizeChanged">

    <ContentDialog x:Name="test" PrimaryButtonText="Ok" SecondaryButtonText="Cancel" Style="{StaticResource AppBarButtonStyle}" Tapped="OnOptionItemTapped" >
        <Image Source="Assets/images/icon_menu.png"/>
    </ContentDialog>
</Page>

【问题讨论】:

  • 如果您想要单击按钮来触发 ContentDialog 的显示,我想您的 XAML 中也会有一个具有 Click 事件的按钮(用于处理 ContentDialog 的显示) 绑定到它...

标签: c# xaml uwp uwp-xaml


【解决方案1】:

可以在您的代码隐藏中轻松创建内容对话框。然后,您可以让 C# 代码在您提到的所需按钮的单击事件上运行。

您的另一个选择是在 XAML 上创建一个内容对话框,正如您已经在原始问题中发布的那样,这样当单击按钮时,您只需参考代码隐藏中的内容对话框并调用 ShowAsync()方法。

我可以在此回复中编写示例代码,但为了让这个问题与将来的访问者相关,我鼓励您参考Microsoft's documentation,这将得到更好的维护。我已经阅读了它,它确实有一些对你有很大帮助的具体例子。

编辑: 发布示例代码作为参考。

MainPage.XAML:

<Grid>
    <Button 
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Height="100" Width="250"
        Content="Show Content"
        FontSize="30px"
        x:Name="ContentBtn"
        Click="ContentBtn_Click"/>

    <ContentDialog x:Name="ContentDialog"
        Title="This is an example"
        PrimaryButtonText="Ok"
        CloseButtonText="Cancel"
        DefaultButton="Primary">
    </ContentDialog>
</Grid>

MainPage.XAML.cs:

private async void ContentBtn_Click(object sender, RoutedEventArgs e) {
        await ContentDialog.ShowAsync(); 
    }

【讨论】:

  • 嘿。所以我稍微了解了一下 MS Docu。我能够创建一个 ContentDialog。我将第二个示例与“termsOfUseContentDialog”一起使用。我不明白如何根据我的需要更改此代码。我做了这样的事情(不完整,没有足够的字符):
  • 我已经用我自己编写和运行的示例更新了我的回复,该示例完全符合您在原始问题中提出的要求。将来询问任何内容时,请避免将代码块作为评论发布,而是选择更新/编辑您的问题,以使其更具可读性。如果是您要找的,请标记为答案!
  • 感谢您的帮助。它成功了。也感谢您的一般提示
  • @axbeit 请记住:要获得更简洁的代码/项目结构,您应该在自己的 .xaml(和 .xaml.cs)中定义 ContentDialog。另一个好处是,您可以在多个页面中重复使用相同的对话框
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 2010-10-06
  • 2011-03-23
  • 2017-06-27
  • 2011-01-29
  • 1970-01-01
相关资源
最近更新 更多