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"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XAMLator.SampleApp.App">
<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>

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

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

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

@ -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));
}
}
}

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

@ -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);
}
}

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

@ -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<bool> Run(Dictionary<Type, object> 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);
});
}
}

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

@ -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
/// </summary>
public class Previewer : IPreviewer
{
bool presented;
protected PreviewPage previewPage;
protected ErrorPage errorPage;
protected Dictionary<Type, object> viewModelsMapping;
bool presented;
ICommand closeCommand;
public Previewer(Dictionary<Type, object> 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;
/// <summary>
/// Preview the specified evaluation result.
/// </summary>