繁体   English   中英

Xamarin形式:Android和IOS中的Toast通知

[英]Xamarin forms: Toast Notification in Android & IOS

嗨,我正在使用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上下文?

为Xamarin尝试了Toast Notification Plugin,但它说.Net版本是不兼容的。

在此输入图像描述

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

您可以参考Xamarin表单的Toast Notifications ,这是示例代码

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

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

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

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

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

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

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

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

谢谢。

我用下面的。 演示在这里找到

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM