自定义Windows Phone 8.1消息对话框

时间:2015-02-10 14:34:36

标签: 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>

2 个答案:

答案 0 :(得分:5)

你所拥有的确切外观是非标准的,如果你想要那个确切的东西,你需要编写一些自定义代码。如果重要部分是警报标题中的图标,则使用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和标题。更高级的版本允许为不同的目的绑定不同的图标。您还可以填充路径而不是绘制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>

这会将OK按钮放在右下方的标准化位置。如果你想将它包含在文本中,你可以像在示例代码中一样将它粘贴在StackPanel中,而不是在ContentDialog上设置PrimaryButtonText。

答案 1 :(得分:0)

在项目中创建Usercontrol。 将整个xaml代码放在Usercontrol中。 现在,您可以将此Usercontrol用作弹出窗口,无论您想在何处使用它。

Popup msgpopup = new Popup( );
msgpopup.child = new CustomisedMessageDialogControl(); //name of ur Usercontrol

简单地打开此对话框,

msgpopup.IsOpen = true;