1

我有警报对话框。Bu默认alertDialog是这样的:

在此处输入图像描述

我想更改确定按钮的颜色并添加边框颜色。这种定制是否有解决方案。这是我的代码:

   await Application.Current.MainPage.DisplayAlert("Alert!", " This is DisplayAlert", "OK");
4

3 回答 3

1

您可以使用像这样的跨平台库:https ://github.com/aritchie/userdialogs

于 2020-02-23T04:35:26.433 回答
0

您将需要创建一个 styles.xml 并以这种方式为 Android 配置它。据我所知,目前无法通过任何 Xamarin.Forms API 自定义本机控件。

例子:

 <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Dialog.Alert">
    <item name="colorAccent">#c7ac56</item>
    <item name="android:textColor">#c7ac56</item>
    <item name="android:background">#5c8487</item>
  </style>

这是关于如何执行此操作的示例的一个很好的教程:http: //gmariotti.blogspot.com/2015/04/a-material-styled-alertdialog.html

如果您使用的是 Xamarin Android,您可能还可以连接到AlertDialog.Builder并以编程方式设置属性:https ://developer.android.com/reference/android/app/AlertDialog.Builder

于 2020-02-21T10:18:41.610 回答
0

您可以使用 [DependencyService] 调用原生 AlerDialog 并在特定平台上对其进行更改,这是一个更改操作按钮颜色的简单示例。

Forms中,创建界面:

public interface IPopUp
{
    void Popup(string title, string message,Color titleColor,Color messageColor,Color OKButtonColor ,EventHandler handler);
}

在 iOS 中

using System;
using System.Collections.Generic;
using System.Linq;



using System.Text;
using App10.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using App10;



[assembly: Dependency(typeof(PopupImplemention))]
namespace App10.iOS
{
    public class PopupImplemention : IPopUp
    {
        public void Popup(string title, string message, Color titleColor, Color messageColor, Color OKButtonColor, EventHandler handler)
        {
            UIAlertController alertController = UIAlertController.Create(title,message,UIAlertControllerStyle.Alert);




            var firstAttributes = new UIStringAttributes
            {
                ForegroundColor =titleColor.ToUIColor(),

            };



            var secondAttributes = new UIStringAttributes
            {
                ForegroundColor =messageColor.ToUIColor(),

            };



            alertController.SetValueForKey(new NSAttributedString(title, firstAttributes), new NSString("attributedTitle"));
            alertController.SetValueForKey(new NSAttributedString(message, secondAttributes), new NSString("attributedMessage"));



            UIAlertAction cancelAction = UIAlertAction.Create("Cancel",UIAlertActionStyle.Cancel,null);
            UIAlertAction okAction = UIAlertAction.Create("OK", UIAlertActionStyle.Default,(sender)=> { handler?.Invoke(sender, new EventArgs()) ; });





            okAction.SetValueForKey(OKButtonColor.ToUIColor(), new NSString("_titleTextColor"));



            alertController.AddAction(cancelAction);
            alertController.AddAction(okAction);



            var currentViewController = topViewControllerWithRootViewController(UIApplication.SharedApplication.Delegate.GetWindow().RootViewController);
            currentViewController.PresentViewController(alertController,true,null);
        }





        UIViewController topViewControllerWithRootViewController(UIViewController rootViewController)
        {
            if (rootViewController is UITabBarController)
            {
                UITabBarController tabBarController = (UITabBarController)rootViewController;
                return topViewControllerWithRootViewController(tabBarController.SelectedViewController);
            }
            else if (rootViewController is UINavigationController)
            {
                UINavigationController navigationController = (UINavigationController)rootViewController;
                return topViewControllerWithRootViewController(navigationController.VisibleViewController);
            }
            else if (rootViewController.PresentedViewController != null)
            {
                UIViewController presentedViewController = rootViewController.PresentedViewController;
                return topViewControllerWithRootViewController(presentedViewController);
            }
            else
            {
                return rootViewController;
            }
        }
    }
}

在安卓中

在 MainActivity

public static MainActivity Intance;

protected override void OnCreate(Bundle savedInstanceState)
{
  TabLayoutResource = Resource.Layout.Tabbar;
  ToolbarResource = Resource.Layout.Toolbar;

  base.OnCreate(savedInstanceState);
  Intance = this;

  Xamarin.Essentials.Platform.Init(this, savedInstanceState);
  global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

  LoadApplication(new App());
}
using Xamarin.Forms;

using xxx;
using xxx.Droid;
using Android;
using System;
using Xamarin.Forms.Platform.Android;
using Android.Support.V7.App;
using Android.Text;

[assembly: Dependency(typeof(PopupImplemention))]
namespace xxx.Droid
{
    public class PopupImplemention : IPopUp
    {
        public void Popup(string title, string message, Color titleColor, Color messageColor, EventHandler handler)
        {

            // because html.string could not support format string , so you need to set the color directly in the string with a static value

            Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(MainActivity.Intance);
            alert.SetTitle(title);
            alert.SetMessage(message);

            alert.SetPositiveButton(Html.FromHtml("<font color='#0000ff'>OK</font>"), (senderAlert, args) =>
            {
                handler?.Invoke(senderAlert, args);
            });
                Android.Support.V7.App.AlertDialog dialog = alert.Create();
            dialog.Show();


        }
    }
}

并以表格形式调用它

 DependencyService.Get<IPopUp>().Popup("Title","xxxxxxxxxxxx",Color.Red,Color.Blue,Color.Green,(sen,args)=> { 

       // handle the logic when  clikc the OK button   

});
于 2020-02-24T06:51:46.290 回答