[c#] Xamarin Forms에 해당하는 토스트

Android 또는 iOS 전용이 아닌 Xamarin Forms를 사용하여 Android가 Toast에서 수행하는 것처럼 사용자 상호 작용이 필요하지 않고 (짧은) 시간이 지나면 사라지는 팝업을 표시하는 방법이 있습니까?

주변을 검색하면 사용자가 클릭해야 사라지는 경고가 표시됩니다.



답변

이에 대한 간단한 해결책이 있습니다. DependencyService 를 사용하면 Android와 iOS 모두에서 Toast-Like 접근 방식을 쉽게 얻을 수 있습니다.

공통 패키지에 인터페이스를 만듭니다.

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();
        }
    }
}

iOS 섹션

iOS에는 Toast와 같은 기본 솔루션이 없으므로 자체 접근 방식을 구현해야합니다.

[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에 클래스를 등록해야합니다.

이제 프로젝트 어디에서나 토스트 서비스에 액세스 할 수 있습니다.

DependencyService.Get<IMessage>().ShortAlert(string message);
DependencyService.Get<IMessage>().LongAlert(string message);


답변

다음 은 여러 메시지가 표시 될 때 UI 고정을 방지하는 Alex Chengalan의 iOS 코드 버전입니다 .

public class MessageIOS : IMessage
    {
        const double LONG_DELAY = 3.5;
        const double SHORT_DELAY = 0.75;

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

        public void ShortAlert(string message)
        {
            ShowAlert(message, SHORT_DELAY);
        }

        void ShowAlert(string message, double seconds)
        {
            var alert = UIAlertController.Create(null, message, UIAlertControllerStyle.Alert);

            var alertDelay = NSTimer.CreateScheduledTimer(seconds, obj =>
            {
                DismissMessage(alert, obj);
            });

            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
        }

        void DismissMessage(UIAlertController alert, NSTimer alertDelay)
        {
            if (alert != null)
            {
                alert.DismissViewController(true, null);
            }

            if (alertDelay != null)
            {
                alertDelay.Dispose();
            }
        }
    }


답변

너겟과 아래와 같은 코드에서 Acr.UserDialogs 패키지를 사용할 수 있습니다.

Acr.UserDialogs.UserDialogs.Instance.Toast(Message, new TimeSpan(3));


답변

우리는 일반적으로 Egors Toasts 플러그인을 사용하지만 현재 프로젝트에 iOS에 대한 권한이 필요하므로 Rg.Plugins.Popup nuget ( https://github.com/rotorgames/Rg.Plugins.Popup을 사용하여 다른 경로로 이동했습니다. ).

PopupPage 유형의 기본 xaml / cs 페이지를 작성했습니다.

<?xml version="1.0" encoding="utf-8" ?>
<popup:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:popup="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
         x:Class="YourApp.Controls.ToastPage">
...

앱 시작시 등록하거나 Xamarin.Forms.DependencyService를 사용하여 서비스를 가져 오는 인터페이스도 사용할 수있는 서비스에 의해 생성됩니다.

서비스 소식은 PopupPage에서 파생 된 페이지로,

await PopupNavigation.PushAsync(newToastPage);
await Task.Delay(2000);
await PopupNavigation.PopAllAsync();

팝업 페이지는 사용자가 페이지 디스플레이 외부를 탭하여 닫을 수 있습니다 (화면을 채우지 않았다고 가정).

이것은 iOS / Droid에서 행복하게 작동하는 것처럼 보이지만, 이것이 위험한 방법 인 것을 아는 사람이 있다면 수정할 수 있습니다.


답변

Alex의 답변에 추가하면 다음은 UWP 변형입니다.

public class Message : IMessage {
  private const double LONG_DELAY = 3.5;
  private const double SHORT_DELAY = 2.0;

  public void LongAlert(string message) =>
    ShowMessage(message, LONG_DELAY);

  public void ShortAlert(string message) =>
    ShowMessage(message, SHORT_DELAY);

  private void ShowMessage(string message, double duration) {
    var label = new TextBlock {
      Text = message,
      Foreground = new SolidColorBrush(Windows.UI.Colors.White),
      HorizontalAlignment = HorizontalAlignment.Center,
      VerticalAlignment = VerticalAlignment.Center,
    };
    var style = new Style { TargetType = typeof(FlyoutPresenter) };
    style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Windows.UI.Colors.Black)));
    style.Setters.Add(new Setter(FrameworkElement.MaxHeightProperty, 1));
    var flyout = new Flyout {
      Content = label,
      Placement = FlyoutPlacementMode.Full,
      FlyoutPresenterStyle = style,
    };

    flyout.ShowAt(Window.Current.Content as FrameworkElement);

    var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(duration) };
    timer.Tick += (sender, e) => {
      timer.Stop();
      flyout.Hide();
    };
    timer.Start();
  }
}

색상과 스타일링은 당신에게 달려 MaxHeight있으며 실제로 높이를 최소로 유지하는 데 필요합니다.


답변

IUserDialog NuGet 을 사용하고 단순히 toastAlert를 사용할 수 있습니다.

var toastConfig = new ToastConfig("Toasting...");
toastConfig.SetDuration(3000);
toastConfig.SetBackgroundColor(System.Drawing.Color.FromArgb(12, 131, 193));

UserDialogs.Instance.Toast(toastConfig);


답변

다음은 Xamarin.iOS에서 토스트를 표시하는 데 사용하는 코드 조각입니다.

  public void ShowToast(String message, UIView view)
    {
        UIView residualView = view.ViewWithTag(1989);
        if (residualView != null)
            residualView.RemoveFromSuperview();

        var viewBack = new UIView(new CoreGraphics.CGRect(83, 0, 300, 100));
        viewBack.BackgroundColor = UIColor.Black;
        viewBack.Tag = 1989;
        UILabel lblMsg = new UILabel(new CoreGraphics.CGRect(0, 20, 300, 60));
        lblMsg.Lines = 2;
        lblMsg.Text = message;
        lblMsg.TextColor = UIColor.White;
        lblMsg.TextAlignment = UITextAlignment.Center;
        viewBack.Center = view.Center;
        viewBack.AddSubview(lblMsg);
        view.AddSubview(viewBack);
        roundtheCorner(viewBack);
        UIView.BeginAnimations("Toast");
        UIView.SetAnimationDuration(3.0f);
        viewBack.Alpha = 0.0f;
        UIView.CommitAnimations();
    }