如何解决没有自定义渲染器,有没有办法以Xamarin形式实现Toast消息
到目前为止,我一直在使用dependency service
在toast messages
中显示Xamarin.Forms
。
现在,我正在寻找一种无需使用toast message
和Xamarin Cross platform
就可以仅在Custom renderer
中开发dependency service
的方法。
有人可以建议我吗
解决方法
您可以尝试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,Text = "Action1",Action = () =>
{
Debug.WriteLine("1");
return Task.CompletedTask;
}
},new SnackBarActionOptions
{
ForegroundColor = Color.Green,BackgroundColor = Color.Red,FontSize = 20,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();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。