button - Xamarin Forms DisplayAlert 按钮 TextColor

标签 button xamarin.forms popupwindow textcolor

如何更改 Xamarin Forms DisplayAlert 对话框上的按钮文本颜色?

最佳答案

FWIW,我选择在 XAML 中创建一个新的 ContentPage 并使用 Navigation.PushModalAsync 显示它。就我而言,这是模拟警报并保持对所有样式的控制的最简单方法。

XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="YourNamespace.AlertPage"
             BackgroundColor="DarkGray"
             Padding="40">
    <ContentPage.Content>
        <StackLayout BackgroundColor="White" Padding="10">
            <Label x:Name="lblTitle" FontAttributes="Bold" FontSize="Large" Text="Title" HorizontalOptions="Center"></Label>
            <BoxView HeightRequest="1" BackgroundColor="DarkGray"></BoxView>
            <ScrollView Orientation="Vertical" VerticalOptions="FillAndExpand">
                <Label x:Name="lblText" FontSize="Medium"></Label>
            </ScrollView>
            <BoxView HeightRequest="1" BackgroundColor="DarkGray"></BoxView>
            <StackLayout Orientation="Horizontal">
                <Button x:Name="btn1" Text="Button 1" HorizontalOptions="CenterAndExpand"></Button>
                <Button x:Name="btn2" Text="Button 2" HorizontalOptions="CenterAndExpand"></Button>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

C#:

public partial class AlertPage : ContentPage
{
    public Label LblTitle
    {
        get
        {
            return lblTitle;
        }
    }

    public Label LblText
    {
        get
        {
            return lblText;
        }
    }

    public Button Button1
    {
        get
        {
            return btn1;
        }
    }

    public Button Button2
    {
        get
        {
            return btn2;
        }
    }

    public AlertPage()
    {
        InitializeComponent();
    }
}

实现:

var ap = new AlertPage();
ap.LblTitle.Text = "Instructions";
ap.LblText.Text = "The text to display!";
ap.Button1.Text = "Done";
ap.Button1.Clicked += async (s, a) =>
{
    await Navigation.PopModalAsync();
};
ap.Button2.IsVisible = false;
await Navigation.PushModalAsync(ap);

截图:

Screenshot from iPhone 7+

关于button - Xamarin Forms DisplayAlert 按钮 TextColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32428364/

相关文章:

php - 检查是否已收到邮寄请求

android - 嵌套的 CollectionViews 和显示(使用 Visual Studio 2019、Xamarin XPlatform Android)

android - 在条目 View 中书写文本时加下划线 - xamarin,forms android

javascript - 从子窗口调用父窗口的JavaScript

android - "Avoid passing null as the view root"带弹出窗口

reactjs - 如何在 React 中通过单击按钮打开 &lt;input type ="file"/>

wpf 按钮背景悬停颜色

css - Twitter Bootstrap TD 无填充

C# 在发布版本中不起作用

android - 如何在 PopupWindow 上显示键盘?