【问题标题】:Is there any way to implement toast message in xamarin forms without custom renderer有没有办法在没有自定义渲染器的情况下以 xamarin 表单实现 toast 消息
【发布时间】:2021-02-15 20:54:07
【问题描述】:

到目前为止,我一直在使用dependency serviceXamarin.Forms 中显示toast messages

现在我正在寻找一种方法,可以在不使用Custom rendererdependency service 的情况下仅在Xamarin Cross platform 中开发toast message

谁能给我建议

【问题讨论】:

    标签: xamarin xamarin.forms cross-platform


    【解决方案1】:

    您可以尝试 ACR 用户对话框块插件。

    https://github.com/aritchie/userdialogs

    我将它用于我的所有项目!

    快速示例

    Acr.UserDialogs.ToastConfig.DefaultPosition = ToastPosition.Top;
    
    Acr.UserDialogs.UserDialogs.Instance.InfoToast("Toast at the top");
    

    【讨论】:

    • 没有插件我们能做什么
    • 如果您不想使用第三方插件,请仅使用依赖服务。我推荐用户对话框,它是一个救生员,它还有一个非常漂亮的加载器!!
    • 创建您自己的控制模板并使用 absolutelayout 放置文本并运行一个计时器功能,该功能可以关闭和打开可见性,之后在您的内容页面上使用控制模板,现在您可以控制您的客户烤面包机
    【解决方案2】:

    我觉得新的https://github.com/xamarin/XamarinCommunityToolkit有些东西

    <?xml version="1.0" encoding="UTF-8"?>
    <pages:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
                    x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.SnackBarPage">
        <StackLayout Spacing="10" Margin="20">
            <Button Clicked="DisplaySnackBarClicked" Text="Show SnackBar"></Button>
            <Button Clicked="DisplayToastClicked" Text="Show toast"></Button>
            <Button Clicked="DisplaySnackBarAdvancedClicked" Text="Show SnackBar"></Button>
            <Label x:Name="StatusText"></Label>
        </StackLayout>
    </pages:BasePage>
    
    
    
    
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Text;
    using System.Threading.Tasks;
    using Xamarin.CommunityToolkit.Extensions;
    using Xamarin.CommunityToolkit.UI.Views.Options;
    using Xamarin.Forms;
    
    namespace Xamarin.CommunityToolkit.Sample.Pages.Views
    {
        public partial class SnackBarPage : BasePage
        {
            public SnackBarPage() => InitializeComponent();
    
            async void DisplaySnackBarClicked(object sender, EventArgs args)
            {
                var result = await this.DisplaySnackBarAsync(GenerateLongText(5), "Run action", () =>
                {
                    Debug.WriteLine("SnackBar action button clicked");
                    return Task.CompletedTask;
                });
                StatusText.Text = result ? "SnackBar is closed by user" : "SnackBar is closed by timeout";
            }
    
            async void DisplayToastClicked(object sender, EventArgs args)
            {
                var result = await this.DisplayToastAsync(GenerateLongText(5));
                StatusText.Text = result ? "SnackBar is closed by user" : "SnackBar is closed by timeout";
            }
    
            async void DisplaySnackBarAdvancedClicked(object sender, EventArgs args)
            {
                var messageOptions = new MessageOptions
                {
                    Foreground = Color.DeepSkyBlue,
                    FontSize = 40,
                    FontFamily = "Sans-serif",
                    Message = GenerateLongText(5)
                };
    
                var actionOptions = new List<SnackBarActionOptions>
                {
                    new SnackBarActionOptions
                    {
                        ForegroundColor = Color.Red,
                        BackgroundColor = Color.Green,
                        FontSize = 40,
                        FontFamily = "Sans-serif",
                        Text = "Action1",
                        Action = () =>
                        {
                            Debug.WriteLine("1");
                            return Task.CompletedTask;
                        }
                    },
                    new SnackBarActionOptions
                    {
                        ForegroundColor = Color.Green,
                        BackgroundColor = Color.Red,
                        FontSize = 20,
                        FontFamily = "Sans-serif",
                        Text = "Action2",
                        Action = () =>
                        {
                            Debug.WriteLine("2");
                            return Task.CompletedTask;
                        }
                    }
                };
                var options = new SnackBarOptions(messageOptions, 5000, Color.Coral, true, actionOptions);
                var result = await this.DisplaySnackBarAsync(options);
                StatusText.Text = result ? "SnackBar is closed by user" : "SnackBar is closed by timeout";
            }
    
            string GenerateLongText(int stringDuplicationTimes)
            {
                const string message = "It is a very long message to test multiple strings. A B C D E F G H I I J K LO P Q R S T U V W X Y Z";
                var result = new StringBuilder();
                for (var i = 0; i < stringDuplicationTimes; i++)
                {
                    result.AppendLine(message);
                }
    
                return result.ToString();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 2013-02-18
      • 2016-04-11
      • 1970-01-01
      • 2022-10-07
      • 2015-07-11
      • 1970-01-01
      相关资源
      最近更新 更多