在Windows Phone通用应用程序中更改内容对话框按钮的样式

Yaw*_*war 2 c# xaml styles windows-phone-8.1 win-universal-app

我在我的xaml中定义了这个内容对话框:

    <ContentDialog x:Name="AlertMessage" Background="#363636" IsSecondaryButtonEnabled="True" SecondaryButtonText="Cancel"  IsPrimaryButtonEnabled="True" PrimaryButtonText="Ok" >
        <ContentDialog.Content>
            <StackPanel Name="rootStackPanel" Height="Auto"  >
                <StackPanel Margin="0">
                    <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="Are you sure you want to log off ?" />
                </StackPanel>
            </StackPanel>
        </ContentDialog.Content>
    </ContentDialog>
Run Code Online (Sandbox Code Playgroud)

而我这样称呼它:

private void AppBarButton_Click(object sender, RoutedEventArgs e)
    {
        MessageBox();
    }
    private async void MessageBox()
    {
        ContentDialogResult LogoutDialog = await AlertMessage.ShowAsync();

        if (LogoutDialog == ContentDialogResult.Primary)
        {
            this.Frame.Navigate(typeof(MainPage));
        }
        else
        {
            // User pressed Cancel or the back arrow.
            // Terms of use were not accepted.
        }

    }
Run Code Online (Sandbox Code Playgroud)

问题:我的应用程序是否具有应用程序中所有按钮的样式.我想将相同的样式应用于该对话框的主要和次要按钮.我无法弄清楚如何实现这一目标.是否可以将样式应用于这些按钮?

Kaz*_*ama 6

我使用此代码更改了 UWP 应用上的按钮颜色。

 var cd = new ContentDialog();
 cd.Content = "Remove file ?";
 cd.Title = "Remove file";
 cd.PrimaryButtonText = "OK";
 cd.SecondaryButtonText = "Cancel";
 var bst = new Windows.UI.Xaml.Style(typeof(Button));
 bst.Setters.Add(new Setter(Button.BackgroundProperty, Colors.Red));
 bst.Setters.Add(new Setter(Button.ForegroundProperty, Colors.White));
 cd.PrimaryButtonStyle = bst;
 var ress = await cd.ShowAsync();
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


小智 5

您可以间接自定义按钮。ContentDialog 的按钮对目标类型“Button”使用“默认”样式。您可以覆盖SDK的ContentDialog Style,在模板的Setter中添加不同的Button Style。此按钮样式仅在 ContentDialog 控件范围内有效。

在这个例子中,新的按钮样式需要放在第一个边框元素中 (x:Name="Container")

<Border.Resources>
  <Style TargetType="Button" BasedOn="{StaticResource YourNormalButtonStyle}">
    <Setter Property="BorderBrush" Value="Red"/>
    <Setter Property="Foreground" Value="Red"/>
  </Style>
</Border.Resources>
Run Code Online (Sandbox Code Playgroud)


小智 5

如果你想在ContenDialog.Resources中更改你需要的ContendDialog PrimaryButtonSettings,只是为了设置你要定位按钮的样式和具有Property =""和Value =""的Setter,下面应该修复的是一个例子.

Converter={StaticResource StringEmptyToBoolConverter}}"
<ContentDialog.Resources>
    <converters:StringEmptyToBoolConverter x:Key="StringEmptyToBoolConverter"/>
    <Style TargetType="Button">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="FontSize" Value="13.6"/>
        <Setter Property="Width" Value="Auto"/>
    </Style>
</ContentDialog.Resources>
<WebView Height="400" Width="500" DefaultBackgroundColor="Transparent" Source="{Binding State.GuideUri}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="RulesWebView"/>
Run Code Online (Sandbox Code Playgroud)