xaml - 自定义 Windows Phone 8.1 消息对话框

标签 xaml windows-phone-8.1

我想自定义我的消息对话框,如下图所示

enter image description here

如何执行我已为此准备了 xaml

   <StackPanel Name="rootStackPanel" Height="Auto"  Background="#363636" VerticalAlignment="Top">
        <StackPanel Margin="10">
            <StackPanel Margin="0,0,0,10" Orientation="Horizontal">
                <TextBlock x:Name="HeadingText" x:FieldModifier="public" Style="{StaticResource ApplicationMessageBoxHeadingStyle}" Text="Alert"  />
                <Image Margin="10,05,0,0" Source="/Assets/Images/alert.png" Width="35"></Image>
            </StackPanel>
            <TextBlock x:FieldModifier="public" x:Name="ContentText" Style="{StaticResource ApplicationMessageBoxErrorStyle}" Text="Pease enter a valid plate number" />
            <Button x:FieldModifier="public"   Name="OkButton" Margin="0,20,0,0" Padding="0"  HorizontalAlignment="Left" Content="Ok"  Style="{StaticResource ApplicationThemeButtonStyle}"/>
        </StackPanel>
    </StackPanel>

最佳答案

您所看到的确切外观是非标准的,如果您想要确切的外观,则需要编写一些自定义代码。如果重要部分是警报标题中的图标,那么使用 ContentDialog 就非常容易。

MessageDialog 不可自定义,但 ContentDialog 可以。有一个模板可以使用 Add.New Item... 菜单向您的项目添加新的 ContentDialog。

获得 ContentDialog 文件后,您可以自定义模板以将其按钮标题为“确定”:

<ContentDialog
    x:Class="MyApp.AlertDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Alert"
    PrimaryButtonText="OK"  
    PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
    >

并将您的alert.png 与标题一起包含在标题模板中。更高级的版本将允许为不同的目的绑定(bind)不同的图标。您还可以填充路径而不是绘制 png,这样图标将更容易缩放。

    <ContentDialog.TitleTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding}" Foreground="{ThemeResource PhoneAccentBrush}"/>
                <Image Source="/Assets/Images/alert.png" />
            </StackPanel>
        </DataTemplate>
    </ContentDialog.TitleTemplate>

然后将其余内容包含在 ContentDialog 的 Xaml 中:

<StackPanel>
    <TextBlock x:FieldModifier="public" x:Name="ContentText" Style="{StaticResource ApplicationMessageBoxErrorStyle}" Text="Pease enter a valid plate number" />
</StackPanel>

这会将“确定”按钮置于右下角的标准化位置。如果您想将其包含在文本中,您可以将其粘贴在 StackPanel 中,就像示例代码中一样,而不是在 ContentDialog 上设置 PrimaryButtonText。

关于xaml - 自定义 Windows Phone 8.1 消息对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28434285/

相关文章:

javascript - navigator.geolocation.getCurrentPosition 在 Windows Phone 8.1 中不起作用

c# - 如何写入 ContactStore.Contact.Phones?

c# - 在 Windows Phone 8.1 运行时将 BitmapImage 转换为 byte[] 数组

wpf - 如何从网格的样式设置网格中任何子元素的前景色?

c# - 无法将 Xaml 中的 SelectedIndex ="0"设置为 ListView(Windows 应用商店应用程序)

c# - 在 c# xaml 中创建一个数学图

xaml - 如何在 (Uno 2.4) 中使用 Microsoft.UI.Xaml.Controls 资源

c# - 如何修复 Windows 应用程序认证套件的调试配置失败?

c# - 适用于 Windows 8.1 的推送通知

c# - 停止 WPF 动画, Storyboard 在 xaml 中开始但在代码隐藏中停止?