using Windows.Foundation;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace HowToAsync1
{
    public sealed partial class MainPage : Page
    {
        Color clr;

        public MainPage()
        {
            this.InitializeComponent();
        }

        void OnButtonClick(object sender, RoutedEventArgs args)
        {
            MessageDialog msgdlg = new MessageDialog("Wybierz kolor", "Asynchronicznie #1");
            msgdlg.Commands.Add(new UICommand("Czerwony", null, Colors.Red));
            msgdlg.Commands.Add(new UICommand("Zielony", null, Colors.Green));
            msgdlg.Commands.Add(new UICommand("Niebieski", null, Colors.Blue));

            // Wyświetlenie okna MessageDialog z procedurą obsługi Completed
            IAsyncOperation<IUICommand> asyncOp = msgdlg.ShowAsync();
            asyncOp.Completed = OnMessageDialogShowAsyncCompleted;
        }

        void OnMessageDialogShowAsyncCompleted(IAsyncOperation<IUICommand> asyncInfo, 
                                               AsyncStatus asyncStatus)
        {
            // Pobranie wartości Color
            IUICommand command = asyncInfo.GetResults();
            clr = (Color)command.Id;

            // Wykorzystanie obiektu Dispatcher w celu uruchomienia w wątku UI
            IAsyncAction asyncAction = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
                                                                OnDispatcherRunAsyncCallback);
        }

        void OnDispatcherRunAsyncCallback()
        {
            // Ustawienie pędzla tła
            contentGrid.Background = new SolidColorBrush(clr);
        }
    }
}
