首页 > 解决方案 > Xamarin 形成风格吐司

问题描述

有没有办法以 xamarin 形式为吐司设计样式?比如改变圆角半径或背景颜色?至少对于安卓来说。我正在使用依赖服务来展示 toast,它工作得很好,但我更喜欢对 toast 进行一些样式化。提前致谢。

安道尔吐司:

[assembly: Xamarin.Forms.Dependency(typeof(MessageAndroid))]
namespace DeliveryApplication.Droid.DependencyService
{
    class MessageAndroid : IMessage
    {
        public void LongToast(string message)
        {
            Toast.MakeText(Application.Context, message, ToastLength.Long).Show();
        }

        public void ShortToast(string message)
        {
            Toast.MakeText(Application.Context, message, ToastLength.Short).Show();
        }
    }
}

IOS吐司:

[assembly: Xamarin.Forms.Dependency(typeof(MessageIOS))] 
namespace DeliveryApplication.Droid.DependencyService
{

    class MessageIOS : IMessage
    {
        const double LONG_DELAY = 3.5;
        const double SHORT_DELAY = 2.0;

        NSTimer alertDelay;
        UIAlertController alert;
        public void LongToast(string message)
        {
            ShowAlert(message, LONG_DELAY);
        }

        public void ShortToast(string message)
        {
            ShowAlert(message, SHORT_DELAY);
        }

        private void ShowAlert(string message, double seconds)
        {
            alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
            {
                dismissMessage();
            });
            alert = UIAlertController.Create(null, message, UIAlertControllerStyle.Alert);
            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
        }

        void dismissMessage()
        {
            if (alert != null)
            {
                alert.DismissViewController(true, null);
            }
            if (alertDelay != null)
            {
                alertDelay.Dispose();
            }
        }
    }
}

标签: c#xamarinxamarin.formsxamarin.androidxamarin.ios

解决方案


推荐阅读