본문 바로가기
Xamarin

[Xamarin] Android Toast Message 구현하기

by LAZY모닝 2022. 11. 28.
반응형

 

 

Xamarin Android Toast Message를 구현해봅시다!

 

 

토스트 메세지(Toast Message)란?

 

아래의 그림과 같이 모바일 환경에서 사용자에게 메세지를알려주는 메세지 박스입니다.

 

 

 

 

토스트 메시지(Toast Message) 구현

 

1. Xamarin 공통 프로젝트 내에 Toast Interface 구현

	public interface IToast
    {
        void LongAlert(string message);
        void ShortAlert(string message);
    }

 

2.   프로젝트명.Andorid 프로젝트 내 기능 구현

[assembly: Xamarin.Forms.Dependency(typeof(MessageAndroid))]
namespace 프로젝트명
{
    public class MessageAndroid : IToast
    {
        public void LongAlert(string message)
        {
            Android.Widget.Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long).Show();
        }

        public void ShortAlert(string message)
        {
            Android.Widget.Toast.MakeText(Android.App.Application.Context, message, ToastLength.Short).Show();
        }
    }
}

 

Toast 메세지 구현

	private void Button_Clicked(object sender, EventArgs e)
        {
            DependencyService.Get<IToast>().ShortAlert("Short Message");
            DependencyService.Get<IToast>().LongAlert("Long Message");
        }

 

 

위의 코드를 실행하면 Button 이벤트 발생시 Toast 메세지가 발생합니다.

구현하고 안되는 부분이 있으시면 댓글 남겨주세요!

 

 

 

 

 

반응형