当前位置: 首页 > 知识库问答 >
问题:

Xamarin形式的吐司等效物

暴乐邦
2023-03-14

在搜索中,我看到的都是需要用户点击才能消失的警报。

共有1个答案

巫马玉堂
2023-03-14

对此有一个简单的解决方案。通过使用DependencyService,您可以轻松地在Android和iOS中获得类似Toast的方法。

在您的公共包中创建一个接口。

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

Android部分

[assembly: Xamarin.Forms.Dependency(typeof(MessageAndroid))]
namespace Your.Namespace
{
    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();
        }
    }
}
[assembly: Xamarin.Forms.Dependency(typeof(MessageIOS))]
namespace Bahwan.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();
            }
        }
    }
}
DependencyService.Get<IMessage>().ShortAlert(string message); 
DependencyService.Get<IMessage>().LongAlert(string message);
 类似资料:
  • 问题内容: 我有一个运行远程服务然后退出的android活动。该服务本身在设备节点上进行轮询并检查更改,我想使用Toast来提醒用户,但是我没有让它起作用。Toast没有显示,过一会儿,Android喊我的应用程序没有响应。顺便说一句,我不想​​再次开始活动并从那里显示吐司,我只是希望它在显示给用户的当前屏幕上弹出。 服务代码如下: 问题答案: 您无法通过服务呼叫Toast消息。除了UI线程之外,

  • 函数功能:在屏幕底部以悬浮层形式显示字符串信息 函数方法 toast(text,time) 参数 类型 必填 说明 text string 是 提示信息,将在设备屏幕上以 HUD 形式显示 time number 否 不写默认 0 - 短时间显示,非 0 - 稍长时间显示 函数示例 toast("欢迎使用积木教程!",2); mSleep(3000);-- 建议 toast 函数后面添加 3 秒

  • 我正在尝试使用Xamarin表单订阅Azure服务总线队列。(说实话,我根本不确定是否有可能做到。) 我可以使用一个简单的控制台应用程序接收来自队列的消息,没有任何问题。但是,当我将相同的代码移动到Xamarin时,它在两种不同的场景中失败。 使用Xamarin是否可以正确订阅Azure服务总线队列? 我是不是漏了什么? 我是否有任何其他选择可以将JSON对象从服务总线发送到电话?

  • 问题内容: 我每次构建Web应用程序时都会想到的问题之一是,消息应该如何显示给最终用户 我尝试了类似Windows应用程序中的消息框,但是它们看起来很糟糕,并且在服务器上发布时会出现问题。我尝试了一个更新面板,该更新面板的页面底部顶部包含一个很酷的标签。.但是我仍然觉得它根本不够好。有时在使用AJAX时在特定情况下会遇到问题,但对于用户来说仍然不太好… 我想问一下出现一段时间然后消失的消息,例如,

  • 我正在尝试用Bootstrap 4创建一个snackbar/toast版本。我从W3Schools的这篇教程开始。 更新:我试图为Bootstrap 4实现一个自定义的snackbar或toast,但现在没有必要,因为Bootstrap 4从4.2版开始就包含了这个选项,正如@zim所说的。