【问题标题】:Xamarin forms: Toast Notification in Android & IOSXamarin 表单:Android 和 IOS 中的 Toast 通知
【发布时间】:2017-02-07 06:02:45
【问题描述】:

您好,我正在使用 PCL 项目以 xamarin 形式创建应用程序。我想在两次后按时实现 Toast 通知,仅适用于 android 和 ios。对于我尝试过的 android -

long doublePressInterval_ms = 300;
DateTime lastPressTime = DateTime.MinValue;
DateTime pressTime = DateTime.Now;

        if ((pressTime - lastPressTime).TotalMilliseconds <= doublePressInterval_ms)
        {
            if(Device.OS == TargetPlatform.Android)
            {

                Java.Lang.JavaSystem.Exit(0);
            }
        }
        else
        {

           Android.Widget.Toast.MakeText(this, string_name, ToastLength.Long).Show();
        }
        lastPressTime = pressTime;
        return false;

但它显示错误无法将页面转换为 Android 上下文。如何在我的 pcl 项目中获取 adnroid 上下文?

我试过Toast Notification Plugin for Xamarin,但它说.Net版本不兼容。

【问题讨论】:

  • @SushiHangover 我尝试了其中提供的所有解决方案。但没有解决办法。
  • Toast.MakeText(Xamarin.Forms.Forms.Context;, string_name, ToastLength.Long).Show();
  • @SushiHangover 我试过了,它说找不到 Xamarin.Forms.Forms.Context
  • @SushiHangover 请参考截图

标签: xamarin xamarin.ios xamarin.android toast


【解决方案1】:

在 Xamarin Android 中,您可以像往常一样显示

Toast.MakeText(this,"toast message", ToastLength.Long).Show();

在 Xamarin iOS 中,您必须使用自定义设计的 UIView 和动画来实现相同的效果

public void ShowToast(String message, UIView view)
{
    UIView residualView = view.ViewWithTag(1989);
    if (residualView != null)
        residualView.RemoveFromSuperview();

    var viewBack = new UIView(new CoreGraphics.CGRect(83, 0, 300, 100));
    viewBack.BackgroundColor = UIColor.Black;
    viewBack.Tag = 1989;
    UILabel lblMsg = new UILabel(new CoreGraphics.CGRect(0, 20, 300, 60));
    lblMsg.Lines = 2;
    lblMsg.Text = message;
    lblMsg.TextColor = UIColor.White;
    lblMsg.TextAlignment = UITextAlignment.Center;
    viewBack.Center = view.Center;
    viewBack.AddSubview(lblMsg);
    view.AddSubview(viewBack);
    roundtheCorner(viewBack);
    UIView.BeginAnimations("Toast");
    UIView.SetAnimationDuration(3.0f);
    viewBack.Alpha = 0.0f;
    UIView.CommitAnimations();
}

【讨论】:

  • 最简单的解决方案~而且完全不阻塞主界面~
  • 如何在iOS上设置UIView view参数?
  • 只需在方法调用时传递视图引用。
【解决方案2】:

您可以参考Toast Notifications for Xamarin Forms,这里是sample code

基本上它在每个平台上使用DependencyService 来实现ToastNotification,而每个平台都有自己的用于烘烤通知的实现。

您可以按照指南完成工作,我遇到的唯一问题是安装此Toasts.Forms.Plugin。当您在 PCL 上安装此软件包时,您可能会遇到此异常:

无法安装包“Toasts.Forms.Plugin 3.1.2”。您正在尝试将此包安装到以“.NETPortable,Version=v4.5,Profile=Profile259”为目标的项目中,但该包不包含任何与该框架兼容的程序集引用或内容文件。

要解决此问题,您可以右键单击 PCL 和“卸载项目”,然后再次右键单击 PCL 并选择“编辑 NAMESPACE.proj”,将代码 &lt;TargetFrameworkProfile&gt;Profile259&lt;/TargetFrameworkProfile&gt; 替换为 &lt;TargetFrameworkProfile&gt;Profile111&lt;/TargetFrameworkProfile&gt;,保存此文件并重新加载这个项目。更改此TargetFrameworkProfile 后,此插件可以成功安装在PCL 上。

【讨论】:

    【解决方案3】:
    public interface IToast
    {
        void Message(string message);
    }
    
    [assembly: Xamarin.Forms.Dependency(typeof(STToast))]
    namespace MasterDetail.Droid.Renderers
    {
        public class STToast: IToast
        {
            public void Message(string message)
            {
                Toast.MakeText(Application.Context, message, ToastLength.Long).Show();
            }
        }
    }
    
    [assembly: Xamarin.Forms.Dependency(typeof(STToast))]
    namespace MasterDetail.iOS.Renderers
    {
        class STToast: IToast
        {
            const double LONG_DELAY = 3.5;
            const double SHORT_DELAY = 2.0;
    
            NSTimer alertDelay;
            UIAlertController alert;
    
            public void Message(string message)
            {
                ShowAlert(message, LONG_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();
                }
            }
        }
    }
    
    //MainPage
    DependencyService.Get<IToast>().Message("Toast Message");
    

    【讨论】:

      【解决方案4】:

      我已经为 Xamarin.Forms(便携式)项目中的 Toast 通知创建了文档。但是,我没有时间在 iPhone 应用程序上工作(坦率地说,我无法在 iPhone 中进行事件检查,因为我没有 Mac ;)),但你可以将它用于 android 应用程序。

      它使用内置功能,您无需为此下载任何外部插件。

      链接:https://docs.google.com/document/d/1C9mrsxvww3RIrm_BrtDWfKZrp6cAvZVqevewznIUHwI/edit?usp=sharing

      示例代码:https://github.com/imchandresh/ToastMessage/tree/master

      谢谢。

      【讨论】:

        【解决方案5】:

        我使用了以下内容。找到演示Here

        using Foundation;
        using UIKit;`
        using Xamarin.Forms;
        using XamStart.Interfaces;
        using XamStart.iOS.DependencyServices;
        using XamStart.Models;
        
        [assembly: Dependency(typeof(ToastService))]
        namespace XamStart.iOS.DependencyServices
        {
            public class ToastService : IToastService
            {
                // Code stolen from here:  http://sezeromer.com/xamarin-forms-ios-toast-mesaj/ 
                const double LONG_DELAY = 3.5;
                const double SHORT_DELAY = 2.0;
        
                NSTimer alertDelay;
                UIAlertController alert;
                public void CookIt(string message, MyToastLength length)
                {
                    var toastLength = (length == MyToastLength.Long) ? LONG_DELAY : SHORT_DELAY;
                    alertDelay = NSTimer.CreateRepeatingScheduledTimer(toastLength, (obj) =>
                    {
                        MesajReddet();
                    });
                    alert = UIAlertController.Create(null, message, UIAlertControllerStyle.Alert);
        
                    UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
                }
        
                void MesajReddet()
                {
                    if (alert != null)
                    {
                        alert.DismissViewController(true, null);
        
                    }
                    if (alertDelay != null)
                    {
                        alertDelay.Dispose();
                    }
                }
            }
        }
        

        【讨论】:

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