如何以 xamarin 形式在屏幕顶部显示小吃店或吐司

How to display a snackbar or toast at top of the screen in xamarin forms

我在 xamarin 项目中实现了 snackbar。 但是我需要在屏幕顶部显示那个小吃店。

代码:

SnackBarOptions options = new SnackBarOptions
{
    MessageOptions = new MessageOptions
    {
        Foreground = Color.Black,
        Message = toastMsg,
        Padding= 15
        
    },
    BackgroundColor = Color.White,
    Duration = TimeSpan.FromSeconds(8),
    CornerRadius = 15,
    IsRtl = true,
    
    
};
Application.Current.MainPage.DisplaySnackBarAsync(options);

您需要编写自定义平台特定代码来实现此目的:

Android:


    GradientDrawable shape = new GradientDrawable();
    shape.SetCornerRadius(15); //For Corner radius
    shape.SetColor(Android.Graphics.Color.Black);
    var contentView = Xamarin.Essentials.Platform.CurrentActivity?.FindViewById(Android.Resource.Id.Content);
    Snackbar snackBar = Snackbar.Make(contentView, "Your Message", 5000);
    Android.Views.View view = snackBar.View;
    view.SetBackground(shape);
    FrameLayout.LayoutParams frameLayout = (FrameLayout.LayoutParams)view.LayoutParameters;
    frameLayout.Gravity = GravityFlags.Top;
    view.LayoutParameters = frameLayout;
    snackBar.Show();

是的,您可以使用 Xamarin.Forms DependencyService 来实现。

请参考以下代码:

1.create 接口 IToast 形式:

public interface IToast
{
    void Show(string message);
}

2.In android,创建 class Toast_Android 以实现接口 IToast 并将重力设置为 GravityFlags.Top:

[assembly: Xamarin.Forms.Dependency(typeof(Toast_Android))]
namespace ToastApp.Droid
{
    public class Toast_Android : IToast
    {
        public void Show(string message)
        {
            Toast toast = Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long);
            View view = toast.View;
            view.SetBackgroundColor(Color.Yellow);
            TextView text = (TextView)view.FindViewById(Android.Resource.Id.Message);
            text.SetTextColor(Color.Red);
            toast.SetGravity(GravityFlags.Top, 0, 0);
            toast.Show();
        }
    }
}

注:

3.In ios, 创建 class Toast_IOS 来实现接口 IToast 并设置视图的位置:

[assembly: Xamarin.Forms.Dependency(typeof(Toast_IOS))]
namespace ToastApp.iOS
{
    public class Toast_IOS : IToast
    {
        const double LONG_DELAY = 3.5;
        NSTimer alertDelay;
        UIViewController alert;

        const float DialogWith = 160;

        public void Show(string message)
        {
            ShowAlert(message, LONG_DELAY);
        }

        void ShowAlert(string message, double seconds)
        {
            alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
            {
                dismissMessage();
            });
            alert = new UIViewController();

            UILabel view = new UILabel();
            int  DeviceWidth = (int)UIScreen.MainScreen.Bounds.Width;

            float position = (DeviceWidth - DialogWith) / 2;

            view.Frame = new CoreGraphics.CGRect(position, 0, DialogWith, 100);
            view.Text = message;

           // you can customize  the style as you want
            view.TextColor = UIColor.Red;
            view.BackgroundColor = UIColor.Yellow;

            alert.View.Add(view);
                UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
        }
        void dismissMessage()
        {
            if (alert != null)
            {
                alert.DismissViewController(true,null);
            }
            if (alertDelay != null)
            {
                alertDelay.Dispose();
            }
        }
    }
}