有没有办法在没有自定义渲染器的情况下以 xamarin 形式实现 toast 消息

Is there any way to implement toast message in xamarin forms without custom renderer

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

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

任何人都可以就此向我提出建议

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

https://github.com/aritchie/userdialogs

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

快速示例

Acr.UserDialogs.ToastConfig.DefaultPosition = ToastPosition.Top;

Acr.UserDialogs.UserDialogs.Instance.InfoToast("Toast at the top");

我觉得新的有东西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();
        }
    }
}