如何在UWP项目中创建ContentDialog

时间:2018-10-23 14:45:45

标签: xamarin uwp xamarin.uwp

我正在编写Xamarin.Forms应用程序,并且在UWP项目中,我想编写特定于平台的代码以提示对话框。我正在尝试像这样编写自定义ContentDialog

<?xml version="1.0" encoding="utf-8" ?>
<controls:ContentDialog
           xmlns:controls="clr-namespace:Windows.UI.Xaml.Controls;assembly=Windows.Foundation.UniversalApiContract"
           x:Class="MyApp.Forms.UWP.Dialogs.SingleInputDialog"
           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"
           x:Name="ContentDialog"
           Title="TITLE"
           mc:Ignorable="d">

</controls:ContentDialog>

当我尝试在其中添加GridTextbox时,智能感知并不表明存在这些控件中的任何一个……我想我缺少了一些琐碎的/显而易见的东西?我尝试从Microsoft文档中复制/粘贴该示例,但我也遇到了同样的问题。

1 个答案:

答案 0 :(得分:2)

您可以创建内容控件并在您的代码中调用

 <ContentDialog
        x:Class="TestButton.MessageDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:TestButton"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="TITLE"
        PrimaryButtonText="Button1"
        SecondaryButtonText="Button2"
        PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
        SecondaryButtonClick="ContentDialog_SecondaryButtonClick">

        <Grid>
        </Grid>
    </ContentDialog>

创建一个类并调用上方的控件(如下所示),然后可以参考下面的函数来显示一个对话框。

 public ContentDialogResult ShowMessage(string message, string primaryButtonText = "OK", string secondaryButtonText = "", bool isSecondaryButtonEnabled = false)
        {
            MessageDialog newDialog = new MessageDialog() { MaxHeight = 1000, MaxWidth = 1000 };
            newDialog.Title = "";
            newDialog.Content = message;
            newDialog.IsSecondaryButtonEnabled = isSecondaryButtonEnabled;
            newDialog.PrimaryButtonText = primaryButtonText;
            newDialog.SecondaryButtonText = secondaryButtonText;
            newDialog.HorizontalAlignment = left;
            return newDialog.ShowAsync().GetResults();
        }

希望有帮助