Xamarin表单:Android中的Toast通知&;国际标准化组织

Xamarin表单:Android中的Toast通知&;国际标准化组织,xamarin,xamarin.ios,xamarin.android,toast,Xamarin,Xamarin.ios,Xamarin.android,Toast,您好,我正在使用PCL项目在xamarin表单中创建一个应用程序。我只想在android和ios上实现两次回按的Toast通知。对于android,我试过了- long doublePressInterval_ms = 300; DateTime lastPressTime = DateTime.MinValue; DateTime pressTime = DateTime.Now; if ((pressTime - lastPressTime).TotalMillisecon

您好,我正在使用PCL项目在xamarin表单中创建一个应用程序。我只想在android和ios上实现两次回按的Toast通知。对于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;
long doublepress interval_ms=300;
DateTime lastPressTime=DateTime.MinValue;
DateTime按time=DateTime.Now;
如果((按Time-lastPressTime).TotalMillistics您可以参考,以下是详细信息

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

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

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


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

我已在Xamarin.Forms(便携式)中创建了Toast通知的文档然而,我没有时间开发iPhone应用程序(坦白地说,我无法在iPhone中进行事件检查,因为我没有Mac;),但是你可以在android应用程序中使用它

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

链接:

示例代码:


谢谢。

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

我用了下面的方法。演示发现

公共接口IToast
{
无效消息(字符串消息);
}
[汇编:Xamarin.Forms.Dependency(typeof(STToast))]
命名空间MasterDetail.Droid.Renderers
{
公共类STToast:IToast
{
公共无效消息(字符串消息)
{
Toast.MakeText(Application.Context,message,ToastLength.Long).Show();
}
}
}
[汇编:Xamarin.Forms.Dependency(typeof(STToast))]
命名空间MasterDetail.iOS.Renderers
{
班级名称:IToast
{
常数双倍长_延迟=3.5;
常数双短_延迟=2.0;
NSTimer警报延迟;
UIAlertController警报;
公共无效消息(字符串消息)
{
ShowAlert(消息、长延时);
}
无效显示警报(字符串消息,双秒)
{
alertDelay=NSTimer.CreateScheduledTimer(秒,(obj)=>
{
dismissMessage();
});
alert=UIAlertController.Create(null,message,UIAlertControllerStyle.alert);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(警报、真、空);
}
void dismissMessage()
{
如果(警报!=null)
{
alert.DismissViewController(true,null);
}
如果(alertDelay!=null)
{
alertDelay.Dispose();
}
}
}
}
//主页
DependencyService.Get()消息(“Toast消息”);

可能重复@SushiHangover我尝试了其中提供的所有解决方案。但是没有解决办法。
Toast.MakeText(Xamarin.Forms.Forms.Context;,string_name,ToastLength.Long).Show()@SushiHangover我试过了,它说找不到Xamarin.Forms.Forms。Context@SushiHangover请参阅ScreenShot这是最简单的解决方案~而且它根本不会阻止主ui ~如何在iOS上设置
UIView视图
参数?只需在方法调用时传递视图参考。
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");