Xamarin Forms DisplayAlert Button TextColor
如何更改Xamarin Forms DisplayAlert对话框上的Button文本颜色?
FWIW,我选择在XAML中创建一个新的ContentPage并使用Navigation.PushModalAsync进行显示。就我而言,这是模拟警报并保持对所有样式的控制的最简单方法。
XAML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?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#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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(); } } |
实施:
1 2 3 4 5 6 7 8 9 10 | 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); |
屏幕截图:
可以在每个平台的自定义渲染器的帮助下更改颜色。
您可以访问自定义渲染器中的本机api。
但是需要确保这是必需的,因为不建议这样做(对于iOS来说是肯定的)。
UIAlertView类旨在按原样使用,并且不支持子类化。此类的视图层次结构是私有的,不能对其进行修改。
此处为iOS相关主题。
我已经创建了自定义displayalert,您可以使用任务回调TaskCompletionSource作为xamarin从中构建DisplayAlert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | public async Task<bool> ShowDialogAsync(string title, string message, string acceptMessage, string cancelMessage) { Grid ShowDialogMessage = null; Grid CurrentPageGrid = (App.Instance.CurrentPage as ContentPage).Content as Grid; TaskCompletionSource<bool> result = new TaskCompletionSource<bool>(); try { ShowDialogMessage = GenericView.CustomDisplayAlert(message, CurrentPageGrid.RowDefinitions.Count, CurrentPageGrid.ColumnDefinitions.Count, () => { //here you can add your implementation CurrentPageGrid.Children.Remove(ShowDialogMessage); result.SetResult(true); }, () => { //here you can add your implementation CurrentPageGrid.Children.Remove(ShowDialogMessage); result.SetResult(false); }, title, acceptMessage, cancelMessage); CurrentPageGrid.Children.Add(ShowDialogMessage); return await result.Task; } catch (Exception ex) { return await App.Current.MainPage.DisplayAlert(title, message, acceptMessage, cancelMessage); #if DEBUG throw ex; #endif } } |
在我的方法中,我添加了实现回调的操作,方法签名可以添加您自己的设计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | public static Grid CustomDisplayAlert(string message, int rows, int columns, Action acceptAction, Action cancelAction, string title ="", string acceptMessage ="", string cancelMessage ="") { Grid overlay = new Grid { BackgroundColor = Color.FromRgba(0, 0, 0, 190), RowDefinitions = new RowDefinitionCollection { new RowDefinition { Height = GridLength.Star }, new RowDefinition { Height = GridLength.Auto }, new RowDefinition { Height = GridLength.Star } } }; Grid.SetRowSpan(overlay, rows); Grid.SetColumnSpan(overlay, columns); Grid bodyView = new Grid(); StackLayout stackMainView = new StackLayout { Margin = new Thickness(20) }; StackLayout stackButtonsView = new StackLayout { Orientation=StackOrientation.Horizontal }; RoundedBox bgOverlay = new RoundedBox { CornerRadius = 4, HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, BackgroundColor = Color.White }; bodyView.Children.Add(bgOverlay); StackLayout elementsContainer = new StackLayout { Margin = new Thickness(10) }; IconicLabel itemDescription = new IconicLabel { MoreReadable = true, Text = message, StyleId ="Myriad-Pro-L", HorizontalTextAlignment = TextAlignment.Center, HorizontalOptions = LayoutOptions.FillAndExpand, TextColor = (Color)(App.Current.Resources["OptioDark"]), FontSize = 18, Margin = new Thickness(15) }; IconicLabel titleView = new IconicLabel { FontAttributes = FontAttributes.Bold, MoreReadable = true, Text = title, StyleId ="Myriad-Pro-L", HorizontalTextAlignment = TextAlignment.Center, HorizontalOptions = LayoutOptions.FillAndExpand, TextColor = (Color)(App.Current.Resources["OptioDark"]), FontSize = 22, Margin = new Thickness(15) }; if (titleView.Text.Length != 0) elementsContainer.Children.Add(titleView); elementsContainer.Children.Add(itemDescription); bodyView.Children.Add(elementsContainer); IconicButton acceptBtn = new IconicButton { HeightRequest = 40, HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, BackgroundColor = Color.White, Text = acceptMessage, TextColor = (Color)(App.Current.Resources["OptioDark"]) }; IconicButton cancelBtn = new IconicButton { HeightRequest = 40, HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, BackgroundColor = Color.White, Text = cancelMessage, TextColor = (Color)(App.Current.Resources["OptioDark"]) }; acceptBtn.Clicked += (sender, e) => { acceptAction?.Invoke(); }; cancelBtn.Clicked += (sender, e) => { cancelAction?.Invoke(); }; stackButtonsView.Children.Add(acceptBtn); stackButtonsView.Children.Add(cancelBtn); stackMainView.Children.Add(bodyView); stackMainView.Children.Add(stackButtonsView); overlay.Children.Add(stackMainView, 0, 1); return overlay; } |