Merge pull request #31 from ylatuya/issue-15

Fixes Issue 15
This commit is contained in:
Andoni Morales Alastruey 2018-11-19 14:09:39 +01:00 коммит произвёл GitHub
Родитель 96a1ee4861 24862879fa
Коммит bcd45a95b5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 97 добавлений и 34 удалений

Просмотреть файл

@ -1,8 +1,27 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms" <Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XAMLator.SampleApp.App"> x:Class="XAMLator.SampleApp.App">
<Application.Resources> <Application.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="TealTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.1*" />
<RowDefinition Height="0.8*" />
<RowDefinition Height="0.1*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.05*" />
<ColumnDefinition Width="0.95*" />
</Grid.ColumnDefinitions>
<BoxView Grid.ColumnSpan="2" Color="Teal" />
<Label Grid.Column="1" Text="{TemplateBinding Parent.HeaderText}" TextColor="White" VerticalOptions="Center" />
<ContentPresenter Grid.Row="1" Grid.ColumnSpan="2" />
<BoxView Grid.Row="2" Grid.ColumnSpan="2" Color="Teal" />
<Label Grid.Row="2" Grid.Column="1" Text="{TemplateBinding Parent.FooterText}" TextColor="White" VerticalOptions="Center" />
</Grid>
</ControlTemplate>
</ResourceDictionary>
</Application.Resources> </Application.Resources>
</Application> </Application>

Просмотреть файл

@ -2,7 +2,8 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XAMLator.SampleApp" xmlns:local="clr-namespace:XAMLator.SampleApp"
x:Class="XAMLator.SampleApp.PageWithCSS"> x:Class="XAMLator.SampleApp.PageWithCSS"
ControlTemplate="{StaticResource TealTemplate}">
<ContentPage.Resources> <ContentPage.Resources>
<StyleSheet Source="/Global.css"/> <StyleSheet Source="/Global.css"/>
</ContentPage.Resources> </ContentPage.Resources>

Просмотреть файл

@ -1,41 +1,71 @@
// //
// Copyright (C) 2018 Fluendo S.A. // Copyright (C) 2018 Fluendo S.A.
using System; using System;
using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Windows.Input;
namespace XAMLator.Server namespace XAMLator.Server
{ {
public class ErrorViewModel : INotifyPropertyChanged public class ErrorViewModel : INotifyPropertyChanged
{ {
private string _title;
private string _error;
private ICommand _closeCommand;
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
string title; public string Title
string error;
public ErrorViewModel (string title, Exception ex)
{ {
this.error = ex.ToString (); get => _title;
this.title = title; set
{
_title = value;
EmitPropertyChanged(nameof(Title));
}
} }
public ErrorViewModel (string title, EvalResult result) public string Error
{ {
error = result.Messages [0].Text; get => _error;
this.title = title; set
{
_error = value;
EmitPropertyChanged(nameof(Error));
}
} }
public string Title { public ICommand CloseCommand
get => title;
}
public string Error {
get => error;
}
void EmitPropertyChanged (string v)
{ {
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));
} }
} }
} }

Просмотреть файл

@ -2,13 +2,16 @@
// Copyright (C) 2018 Fluendo S.A. // Copyright (C) 2018 Fluendo S.A.
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input;
namespace XAMLator.Server namespace XAMLator.Server
{ {
public interface IPreviewer public interface IPreviewer
{ {
Task Preview (EvalResult res); ICommand CloseCommand { get; }
Task NotifyError (ErrorViewModel errorViewModel); Task Preview(EvalResult res);
Task NotifyError(ErrorViewModel errorViewModel);
} }
} }

Просмотреть файл

@ -22,6 +22,7 @@ namespace XAMLator.Server
IPreviewer previewer; IPreviewer previewer;
bool isRunning; bool isRunning;
TcpCommunicatorClient client; TcpCommunicatorClient client;
ErrorViewModel errorViewModel;
internal static PreviewServer Instance => serverInstance; internal static PreviewServer Instance => serverInstance;
@ -29,6 +30,7 @@ namespace XAMLator.Server
{ {
client = new TcpCommunicatorClient(); client = new TcpCommunicatorClient();
client.DataReceived += HandleDataReceived; client.DataReceived += HandleDataReceived;
errorViewModel = new ErrorViewModel();
} }
public static Task<bool> Run(Dictionary<Type, object> viewModelsMapping = null, IPreviewer previewer = null) public static Task<bool> Run(Dictionary<Type, object> viewModelsMapping = null, IPreviewer previewer = null)
@ -54,6 +56,7 @@ namespace XAMLator.Server
previewer = new Previewer(viewModelsMapping); previewer = new Previewer(viewModelsMapping);
} }
this.previewer = previewer; this.previewer = previewer;
errorViewModel.CloseCommand = previewer.CloseCommand;
vm = new VM(); vm = new VM();
isRunning = true; isRunning = true;
return true; return true;
@ -115,7 +118,8 @@ namespace XAMLator.Server
} }
catch (Exception ex) 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); tcs.SetException(ex);
} }
}); });
@ -125,7 +129,8 @@ namespace XAMLator.Server
{ {
Xamarin.Forms.Device.BeginInvokeOnMainThread(async () => 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);
}); });
} }
} }

Просмотреть файл

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms; using Xamarin.Forms;
namespace XAMLator.Server namespace XAMLator.Server
@ -11,27 +12,31 @@ namespace XAMLator.Server
/// </summary> /// </summary>
public class Previewer : IPreviewer public class Previewer : IPreviewer
{ {
bool presented;
protected PreviewPage previewPage; protected PreviewPage previewPage;
protected ErrorPage errorPage; protected ErrorPage errorPage;
protected Dictionary<Type, object> viewModelsMapping; protected Dictionary<Type, object> viewModelsMapping;
bool presented;
ICommand closeCommand;
public Previewer(Dictionary<Type, object> viewModelsMapping) public Previewer(Dictionary<Type, object> viewModelsMapping)
{ {
this.viewModelsMapping = viewModelsMapping; this.viewModelsMapping = viewModelsMapping;
errorPage = new ErrorPage();
closeCommand = new Command(() =>
{
HidePreviewPage(previewPage);
presented = false;
});
var quitLive = new ToolbarItem var quitLive = new ToolbarItem
{ {
Text = "Quit live preview", Text = "Quit live preview",
Command = new Command(() => Command = closeCommand
{
HidePreviewPage(previewPage);
presented = false;
}),
}; };
previewPage = new PreviewPage(quitLive); previewPage = new PreviewPage(quitLive);
errorPage = new ErrorPage();
} }
public ICommand CloseCommand => closeCommand;
/// <summary> /// <summary>
/// Preview the specified evaluation result. /// Preview the specified evaluation result.
/// </summary> /// </summary>