Xamarin表格:Ios Toast通知

时间:2017-03-21 10:17:51

标签: ios xamarin xamarin.ios

我在Xamarin PCL项目中创建了一个Toast通知。我使用this创建了此控件。一旦此Toast消息消失,我的应用程序将变为空白。我想不通为什么?任何地方都没有例外吗?

便携式:

namespace ABC
{
        public interface IMessage
        {
            void LongAlert(string message);
            void ShortAlert(string message);
        }
}

在Droid中:

public class MessageAndroid : IMessage
    {
        public void LongAlert(string message)
        {
            Toast.MakeText(Application.Context, message, ToastLength.Long).Show();
        }

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

在Windows 10中:

public class ToastNotificationManagerRenderer : IMessage
    {
        public void LongAlert(string message)
        {
            var notificationXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
            var toeastElement = notificationXml.GetElementsByTagName("text");
            toeastElement[0].AppendChild(notificationXml.CreateTextNode(message));
            var toastNotification = new ToastNotification(notificationXml);
            ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
        }

        public void ShortAlert(string message)
        {
            var notificationXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
            var toeastElement = notificationXml.GetElementsByTagName("text");
            toeastElement[0].AppendChild(notificationXml.CreateTextNode(message));
            var toastNotification = new ToastNotification(notificationXml);
            ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
        }
    }

在Ios中:

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

        NSTimer alertDelay;
        UIAlertController alert;

        public void LongAlert(string message)
        {
            ShowAlert(message, LONG_DELAY);
        }
        public void ShortAlert(string message)
        {
            ShowAlert(message, SHORT_DELAY);
        }

        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();
            }
        }
    }

对于Ios:我也试过这个插件。MessageBarLib。在便携式但与Ios相同的代码 -

public void ShortAlert(string message)
        {
            MessageBarManager.SharedInstance.ShowMessage("Success", message, MessageType.Success);

        }

但退出上述功能后,我的应用程序将关闭。

2 个答案:

答案 0 :(得分:0)

根据您的评论和代码,我怀疑dismissMessage()'正在清除由' PresentingViewController'放入堆栈的所有UIViewControllers。我怀疑它有点像一个bug,但如果你尝试:

PresentingViewController.DismissViewController(true, null);

而不是

alert.DismissViewController(true, null);

我怀疑它应该正常工作。如果没有,那么我们可以更多地了解您在iOS项目中使用的设计模式,以确定您是否正在呈现' UIViewControllers'正常。

答案 1 :(得分:0)

我为IOS尝试了这个: -

private const int Margin = 30;
private const int Height = 40;
private const int Width = 400;
private NSTimer _timer;

    public void ShowAlert(string message)
    {
        var toast = new MessageIOS();
        toast.Show(UIApplication.SharedApplication.KeyWindow.RootViewController.View, message);
    }
    public MessageIOS()
    {
        _view = new UIView(new CGRect(0, 0, 0, 0))
        {
            BackgroundColor = UIColor.FromRGB(0, 175, 240)
        };
        _view.Layer.CornerRadius = (nfloat)20.0;

        _label = new UILabel(new CGRect(0, 0, 0, 0))
        {
            TextAlignment = UITextAlignment.Center,
            TextColor = UIColor.White
        };
        _view.AddSubview(_label);

    }

    public void Show(UIView parent, string message)
    {
        if (_timer != null)
        {
            _timer.Invalidate();
            _view.RemoveFromSuperview();
        }

        _view.Alpha = (nfloat)0.7;

        _view.Frame = new CGRect(
            (parent.Bounds.Width - Width) / 2,
            parent.Bounds.Height - Height - Margin,
            Width,
            Height);

        _label.Frame = new CGRect(0, 0, Width, Height);
        _label.Text = message;

        parent.AddSubview(_view);

        var wait = 10;
        _timer = NSTimer.CreateRepeatingScheduledTimer(TimeSpan.FromMilliseconds(100), delegate {
            if (_view.Alpha <= 0)
            {
                _timer.Invalidate();
                _view.RemoveFromSuperview();
            }
            else
            {
                if (wait > 0)
                {
                    wait--;
                }
                else
                {
                    _view.Alpha -= (nfloat)0.05;
                }
            }
        });
    }