【问题标题】:Change the style of content dialog button in windows phone universal application在 windows phone 通用应用程序中更改内容对话框按钮的样式
【发布时间】:2015-04-16 18:55:33
【问题描述】:

我在我的 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>

我这样称呼它:

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.
        }

    }

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

【问题讨论】:

    标签: c# xaml windows-phone-8.1 styles win-universal-app


    【解决方案1】:

    我使用此代码更改了我的 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();
    

    【讨论】:

      【解决方案2】:

      如果您想更改 ContenDialog.Resources 中所需的 ContendDialog PrimaryButtonSettings 只是为了设置您定位为 Buttons 的 Style 以及 With 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"/>
      

      【讨论】:

      • 这不起作用。你在谈论 ContentDialog 按钮吗?或 ContentDialog 上的任何自定义按钮?
      • 我说的是默认的 ContendDialog PrimaryButtonSettings,
      【解决方案3】:

      您可以间接自定义按钮。 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>
      

      【讨论】:

        【解决方案4】:

        ContentDialog 按钮不可自定义,但您可以不设置 ContentDialog 的主要和次要按钮,并在模板中添加您自己的按钮。

        【讨论】:

        • 这就是我已经做过的 :) 但我认为这不是一个好习惯,但你也提出了同样的建议,这就是我将其标记为答案的原因。
        • 当我在 contentDialog 中使用 Grid 时,它在左侧和右侧添加了一个边距,因此内容不完全可见?有什么解决办法吗?
        • 如果是,那么 PrimaryButtonStyle 使用什么?
        猜你喜欢
        • 1970-01-01
        • 2014-05-12
        • 1970-01-01
        • 2016-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多