우선은 MVVMLight 를 사용해 보았어야 함.


공유프로젝트에 Helpers 폴더 만들고 그 안에 DialogService.cs  파일 추가. 아래 내용으로 채우기

using System;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Views;
using Xamarin.Forms;
 
namespace Helpers
{
    public class DialogService : IDialogService
    {
        private Page _dialogPage;
 
        public void Initialize(Page dialogPage)
        {
            _dialogPage = dialogPage;
        }
 
        public async Task ShowError(string message,
            string title,
            string buttonText,
            Action afterHideCallback)
        {
            await _dialogPage.DisplayAlert(
                title,
                message,
                buttonText);
 
            if (afterHideCallback != null)
            {
                afterHideCallback();
            }
        }
 
        public async Task ShowError(
            Exception error,
            string title,
            string buttonText,
            Action afterHideCallback)
        {
            await _dialogPage.DisplayAlert(
                title,
                error.Message,
                buttonText);
 
            if (afterHideCallback != null)
            {
                afterHideCallback();
            }
        }
 
        public async Task ShowMessage(
            string message,
            string title)
        {
            await _dialogPage.DisplayAlert(
                title,
                message,
                "OK");
        }
 
        public async Task ShowMessage(
            string message,
            string title,
            string buttonText,
            Action afterHideCallback)
        {
            await _dialogPage.DisplayAlert(
                title,
                message,
                buttonText);
 
            if (afterHideCallback != null)
            {
                afterHideCallback();
            }
        }
 
        public async Task<bool> ShowMessage(
            string message,
            string title,
            string buttonConfirmText,
            string buttonCancelText,
            Action<bool> afterHideCallback)
        {
            var result = await _dialogPage.DisplayAlert(
                title,
                message,
                buttonConfirmText,
                buttonCancelText);
 
            if (afterHideCallback != null)
            {
                afterHideCallback(result);
            }
 
            return result;
        }
 
        public async Task ShowMessageBox(
            string message,
            string title)
        {
            await _dialogPage.DisplayAlert(
                title,
                message,
                "OK");
        }
    }
}


다음으로 ViewModelLocator.cs가 있을것이다. 아래와 같이 IDialogService 인터페이스의 DialogService 구현을 등록한다.

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Views;
using Helpers;
using Microsoft.Practices.ServiceLocation;
 
namespace WordTESTApp.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// </summary>
    public class ViewModelLocator
    {
        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
 
            SimpleIoc.Default.Register<IDialogService, DialogService>();
            SimpleIoc.Default.Register<LoginViewModel>();
            SimpleIoc.Default.Register<MainViewModel>();
        }
 
        public LoginViewModel LoginViewModel
        {
            get { return ServiceLocator.Current.GetInstance<LoginViewModel>(); }
        }
 
        public MainViewModel MainViewModel
        {
            get { return ServiceLocator.Current.GetInstance<MainViewModel>(); }
        }
 
        public static void Cleanup()
        {
            // TODO Clear the ViewModels
        }
    }
}


뷰모델에는 다음과 같은 형태로 사용

public class LoginViewModel
{
    private readonly IDialogService _dialogService;
 
    public RelayCommand LoginCommand { get; set; }
 
    public LoginViewModel(IDialogService dialogService)
    {
        _dialogService = dialogService;
        LoginCommand = new RelayCommand(LoginAction);
    }
 
    void LoginAction()
    {
        _dialogService.ShowMessageBox("클릭", "");
    }
 
}



+ Recent posts