【问题标题】:Xamarin Form: Ios Toast NotificationXamarin 表单:Ios Toast 通知
【发布时间】:2017-03-24 11:57:38
【问题描述】:

我在我的 Xamarin PCL 项目中创建了一个 Toast 通知。我使用this 创建了这个控件。一旦这个 toast 消息消失,我的应用程序就会变成空白。我不知道为什么?哪里都不例外??

便携式:

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

在机器人中:

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

        }

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

【问题讨论】:

  • 理想情况下,我们可以使用一些代码来查看,但我建议当模式通知从视图中删除时,基本 UIViewController 可能正在运行它的“ViewWillAppear”或“ViewDidLoad”方法我冒昧地猜测你里面有一些它不喜欢的东西。如果您可以给我们您的代码示例而不是您的工作内容,那么确认这一点会更容易。
  • 每个版本都关闭吗? IE。 iOS、Android 和 Windows?我可以在您的 iOS 课程中看到一些可能会混淆的地方,但让我们尝试进一步缩小范围。
  • @Digitalsa1nt 仅在 IOS 中发生。
  • @Digitalsa1nt 看看我的回答。

标签: ios xamarin xamarin.ios


【解决方案1】:

根据您的 cmets 和代码,我怀疑dismissMessage()' 正在清除所有由'PresentingViewController' 放入堆栈的UIViewController。我怀疑这是一个错误,但如果你尝试:

PresentingViewController.DismissViewController(true, null);

代替

alert.DismissViewController(true, null);

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

【讨论】:

    【解决方案2】:

    我在 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;
                    }
                }
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      相关资源
      最近更新 更多