diff --git a/SampleApp/XAMLator.SampleApp/App.xaml b/SampleApp/XAMLator.SampleApp/App.xaml index 1ce9187..7d15c58 100644 --- a/SampleApp/XAMLator.SampleApp/App.xaml +++ b/SampleApp/XAMLator.SampleApp/App.xaml @@ -1,8 +1,27 @@ - + - + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SampleApp/XAMLator.SampleApp/PageWithCSS.xaml b/SampleApp/XAMLator.SampleApp/PageWithCSS.xaml index 5a6c535..41096a5 100644 --- a/SampleApp/XAMLator.SampleApp/PageWithCSS.xaml +++ b/SampleApp/XAMLator.SampleApp/PageWithCSS.xaml @@ -2,7 +2,8 @@ + x:Class="XAMLator.SampleApp.PageWithCSS" + ControlTemplate="{StaticResource TealTemplate}"> diff --git a/XAMLator.Server.Shared/ErrorViewModel.cs b/XAMLator.Server.Shared/ErrorViewModel.cs index 15e78f0..5705fd9 100644 --- a/XAMLator.Server.Shared/ErrorViewModel.cs +++ b/XAMLator.Server.Shared/ErrorViewModel.cs @@ -1,41 +1,71 @@ // // Copyright (C) 2018 Fluendo S.A. using System; -using System.Collections.ObjectModel; using System.ComponentModel; +using System.Windows.Input; namespace XAMLator.Server { public class ErrorViewModel : INotifyPropertyChanged { + private string _title; + private string _error; + private ICommand _closeCommand; + public event PropertyChangedEventHandler PropertyChanged; - string title; - string error; - - public ErrorViewModel (string title, Exception ex) + public string Title { - this.error = ex.ToString (); - this.title = title; + get => _title; + set + { + _title = value; + EmitPropertyChanged(nameof(Title)); + } } - public ErrorViewModel (string title, EvalResult result) + public string Error { - error = result.Messages [0].Text; - this.title = title; + get => _error; + set + { + _error = value; + EmitPropertyChanged(nameof(Error)); + } } - public string Title { - get => title; - } - - public string Error { - get => error; - } - - void EmitPropertyChanged (string v) + public ICommand CloseCommand { - PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (v)); + get => _closeCommand; + set + { + _closeCommand = value; + EmitPropertyChanged(nameof(CloseCommand)); + } + } + + public void SetError(string title, Exception ex) + { + Error = ex.ToString(); + Title = title; + } + + public void SetError(string title, EvalResult result) + { + if (result.Messages.Length > 0) + { + Error = result.Messages[0].Text; + } + else + { + Error = "Unknown error"; + } + Title = title; + } + + void EmitPropertyChanged(string v) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v)); } } } diff --git a/XAMLator.Server.Shared/IPreviewer.cs b/XAMLator.Server.Shared/IPreviewer.cs index 3a38643..fb0cf09 100644 --- a/XAMLator.Server.Shared/IPreviewer.cs +++ b/XAMLator.Server.Shared/IPreviewer.cs @@ -2,13 +2,16 @@ // Copyright (C) 2018 Fluendo S.A. using System; using System.Threading.Tasks; +using System.Windows.Input; namespace XAMLator.Server { public interface IPreviewer { - Task Preview (EvalResult res); + ICommand CloseCommand { get; } - Task NotifyError (ErrorViewModel errorViewModel); + Task Preview(EvalResult res); + + Task NotifyError(ErrorViewModel errorViewModel); } } diff --git a/XAMLator.Server.Shared/PreviewServer.cs b/XAMLator.Server.Shared/PreviewServer.cs index 2c0b958..6b5a0ac 100644 --- a/XAMLator.Server.Shared/PreviewServer.cs +++ b/XAMLator.Server.Shared/PreviewServer.cs @@ -22,6 +22,7 @@ namespace XAMLator.Server IPreviewer previewer; bool isRunning; TcpCommunicatorClient client; + ErrorViewModel errorViewModel; internal static PreviewServer Instance => serverInstance; @@ -29,6 +30,7 @@ namespace XAMLator.Server { client = new TcpCommunicatorClient(); client.DataReceived += HandleDataReceived; + errorViewModel = new ErrorViewModel(); } public static Task Run(Dictionary viewModelsMapping = null, IPreviewer previewer = null) @@ -54,6 +56,7 @@ namespace XAMLator.Server previewer = new Previewer(viewModelsMapping); } this.previewer = previewer; + errorViewModel.CloseCommand = previewer.CloseCommand; vm = new VM(); isRunning = true; return true; @@ -115,7 +118,8 @@ namespace XAMLator.Server } catch (Exception ex) { - await previewer.NotifyError(new ErrorViewModel("Oh no! An exception!", ex)); + errorViewModel.SetError("Oh no! An exception!", ex); + await previewer.NotifyError(errorViewModel); tcs.SetException(ex); } }); @@ -125,7 +129,8 @@ namespace XAMLator.Server { Xamarin.Forms.Device.BeginInvokeOnMainThread(async () => { - await previewer.NotifyError(new ErrorViewModel("Oh no! An evaluation error!", result)); + errorViewModel.SetError("Oh no! An evaluation error!", result); + await previewer.NotifyError(errorViewModel); }); } } diff --git a/XAMLator.Server.Shared/Previewer.cs b/XAMLator.Server.Shared/Previewer.cs index 697511a..ec9106a 100644 --- a/XAMLator.Server.Shared/Previewer.cs +++ b/XAMLator.Server.Shared/Previewer.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; +using System.Windows.Input; using Xamarin.Forms; namespace XAMLator.Server @@ -11,27 +12,31 @@ namespace XAMLator.Server /// public class Previewer : IPreviewer { - bool presented; protected PreviewPage previewPage; protected ErrorPage errorPage; protected Dictionary viewModelsMapping; + bool presented; + ICommand closeCommand; public Previewer(Dictionary viewModelsMapping) { this.viewModelsMapping = viewModelsMapping; + errorPage = new ErrorPage(); + closeCommand = new Command(() => + { + HidePreviewPage(previewPage); + presented = false; + }); var quitLive = new ToolbarItem { Text = "Quit live preview", - Command = new Command(() => - { - HidePreviewPage(previewPage); - presented = false; - }), + Command = closeCommand }; previewPage = new PreviewPage(quitLive); - errorPage = new ErrorPage(); } + public ICommand CloseCommand => closeCommand; + /// /// Preview the specified evaluation result. ///