2018-09-02 04:56:35 +00:00
|
|
|
|
using Ooui;
|
|
|
|
|
using System;
|
2018-03-13 03:56:12 +00:00
|
|
|
|
using System.Threading;
|
2017-11-26 19:30:19 +00:00
|
|
|
|
using Xamarin.Forms;
|
2018-03-13 03:56:12 +00:00
|
|
|
|
using Xamarin.Forms.Xaml;
|
2018-09-02 04:56:35 +00:00
|
|
|
|
using Color = Xamarin.Forms.Color;
|
|
|
|
|
using Label = Xamarin.Forms.Label;
|
2017-11-26 19:30:19 +00:00
|
|
|
|
|
|
|
|
|
namespace Samples
|
|
|
|
|
{
|
|
|
|
|
public class XamlPreviewPageSample : ISample
|
|
|
|
|
{
|
|
|
|
|
public string Title => "Xamarin.Forms XAML Editor";
|
2018-09-02 04:56:35 +00:00
|
|
|
|
public string Path => "/xaml-editor";
|
2017-11-26 19:30:19 +00:00
|
|
|
|
|
|
|
|
|
public Ooui.Element CreateElement ()
|
|
|
|
|
{
|
2018-03-13 03:56:12 +00:00
|
|
|
|
var page = new XamlEditorPage ();
|
2017-11-26 19:30:19 +00:00
|
|
|
|
return page.GetOouiElement ();
|
|
|
|
|
}
|
2018-09-02 04:56:35 +00:00
|
|
|
|
|
|
|
|
|
public void Publish()
|
|
|
|
|
{
|
|
|
|
|
UI.Publish(Path, CreateElement);
|
|
|
|
|
}
|
2017-11-26 19:30:19 +00:00
|
|
|
|
}
|
2018-03-13 03:56:12 +00:00
|
|
|
|
|
|
|
|
|
public partial class XamlEditorPage : ContentPage
|
|
|
|
|
{
|
|
|
|
|
Editor editor;
|
|
|
|
|
ContentView results;
|
|
|
|
|
|
|
|
|
|
public XamlEditorPage ()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent ();
|
|
|
|
|
|
|
|
|
|
editor.Text = @"<ContentView
|
|
|
|
|
xmlns=""http://xamarin.com/schemas/2014/forms""
|
|
|
|
|
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"">
|
|
|
|
|
<Grid>
|
|
|
|
|
<Grid.RowDefinitions>
|
|
|
|
|
<RowDefinition Height=""*"" />
|
|
|
|
|
<RowDefinition Height=""*"" />
|
|
|
|
|
</Grid.RowDefinitions>
|
|
|
|
|
<Grid.ColumnDefinitions>
|
|
|
|
|
<ColumnDefinition Width=""*"" />
|
|
|
|
|
<ColumnDefinition Width=""*"" />
|
|
|
|
|
</Grid.ColumnDefinitions>
|
|
|
|
|
<StackLayout Grid.Row=""0"" Grid.Column=""0"">
|
|
|
|
|
<Label Text=""Top Left"" />
|
|
|
|
|
<Entry Placeholder=""I'm ready for some text"" />
|
|
|
|
|
<Button Text=""I'm a button, but I don't do anything"" />
|
|
|
|
|
</StackLayout>
|
|
|
|
|
<Label Text=""Top Right"" Grid.Row=""0"" Grid.Column=""1"" TextColor=""White"" BackgroundColor=""#c5000b"" />
|
|
|
|
|
<Label Text=""Bottom Left"" Grid.Row=""1"" Grid.Column=""0"" TextColor=""Black"" BackgroundColor=""#ffd320"" />
|
|
|
|
|
<Label Text=""Bottom Right"" Grid.Row=""1"" Grid.Column=""1"" TextColor=""White"" BackgroundColor=""#008000"" />
|
|
|
|
|
</Grid>
|
|
|
|
|
</ContentView>";
|
|
|
|
|
editor.TextChanged += (sender, e) => DisplayXaml ();
|
|
|
|
|
DisplayXaml ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InitializeComponent ()
|
|
|
|
|
{
|
|
|
|
|
var grid = new Grid ();
|
|
|
|
|
|
|
|
|
|
grid.RowDefinitions.Add (new RowDefinition { Height = GridLength.Auto });
|
|
|
|
|
grid.RowDefinitions.Add (new RowDefinition { Height = GridLength.Star });
|
|
|
|
|
grid.ColumnDefinitions.Add (new ColumnDefinition { Width = GridLength.Star });
|
|
|
|
|
grid.ColumnDefinitions.Add (new ColumnDefinition { Width = GridLength.Star });
|
|
|
|
|
|
|
|
|
|
editor = new Editor {
|
|
|
|
|
FontSize = 12,
|
|
|
|
|
FontFamily = "monospace",
|
|
|
|
|
};
|
|
|
|
|
editor.SetValue (Grid.ColumnProperty, 0);
|
|
|
|
|
editor.SetValue (Grid.RowProperty, 1);
|
|
|
|
|
|
|
|
|
|
results = new ContentView ();
|
|
|
|
|
results.SetValue (Grid.ColumnProperty, 1);
|
|
|
|
|
results.SetValue (Grid.RowProperty, 1);
|
|
|
|
|
|
|
|
|
|
var title = new Label {
|
|
|
|
|
Text = "XAML Editor",
|
|
|
|
|
FontSize = 24,
|
|
|
|
|
FontAttributes = FontAttributes.Bold,
|
|
|
|
|
Margin = new Thickness (8),
|
|
|
|
|
};
|
|
|
|
|
title.SetValue (Grid.ColumnProperty, 0);
|
|
|
|
|
title.SetValue (Grid.RowProperty, 0);
|
|
|
|
|
|
|
|
|
|
grid.Children.Add (title);
|
|
|
|
|
grid.Children.Add (editor);
|
|
|
|
|
grid.Children.Add (results);
|
|
|
|
|
|
|
|
|
|
Content = grid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CancellationTokenSource lastCts = null;
|
|
|
|
|
|
|
|
|
|
public void DisplayXaml ()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
var cts = new CancellationTokenSource ();
|
|
|
|
|
var token = cts.Token;
|
|
|
|
|
lastCts?.Cancel ();
|
|
|
|
|
lastCts = cts;
|
|
|
|
|
|
|
|
|
|
var contentView = new ContentView ();
|
|
|
|
|
contentView.LoadFromXaml (editor.Text);
|
|
|
|
|
|
|
|
|
|
if (!token.IsCancellationRequested) {
|
|
|
|
|
results.Content = contentView;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException) {
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
2018-09-02 04:56:35 +00:00
|
|
|
|
results.Content = new Label
|
|
|
|
|
{
|
2018-03-13 03:56:12 +00:00
|
|
|
|
TextColor = Color.DarkRed,
|
|
|
|
|
FontSize = 12,
|
|
|
|
|
Text = ex.ToString (),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-26 19:30:19 +00:00
|
|
|
|
}
|