Add Entry and XAML Preview sample

This commit is contained in:
Frank A. Krueger 2017-11-26 11:30:19 -08:00
parent 6dc869b9e5
commit 8317b5244b
6 changed files with 220 additions and 4 deletions

View File

@ -9,6 +9,7 @@ using Xamarin.Forms.Internals;
[assembly: ExportRenderer (typeof (BoxView), typeof (BoxRenderer))] [assembly: ExportRenderer (typeof (BoxView), typeof (BoxRenderer))]
[assembly: ExportRenderer (typeof (Button), typeof (ButtonRenderer))] [assembly: ExportRenderer (typeof (Button), typeof (ButtonRenderer))]
[assembly: ExportRenderer (typeof (DatePicker), typeof (DatePickerRenderer))] [assembly: ExportRenderer (typeof (DatePicker), typeof (DatePickerRenderer))]
[assembly: ExportRenderer (typeof (Editor), typeof (EditorRenderer))]
[assembly: ExportRenderer (typeof (Entry), typeof (EntryRenderer))] [assembly: ExportRenderer (typeof (Entry), typeof (EntryRenderer))]
[assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))] [assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))]
[assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))] [assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))]

View File

@ -0,0 +1,129 @@
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Ooui.Forms.Extensions;
namespace Ooui.Forms.Renderers
{
public class EditorRenderer : ViewRenderer<Editor, TextArea>
{
bool _disposed;
IEditorController ElementController => Element;
protected override void Dispose (bool disposing)
{
if (_disposed)
return;
_disposed = true;
if (disposing) {
if (Control != null) {
Control.Changed -= HandleChanged;
//Control.Started -= OnStarted;
//Control.Ended -= OnEnded;
}
}
base.Dispose (disposing);
}
protected override void OnElementChanged (ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged (e);
if (e.NewElement == null)
return;
if (Control == null) {
SetNativeControl (new TextArea {
ClassName = "form-control"
});
Control.Changed += HandleChanged;
//Control.Started += OnStarted;
//Control.Ended += OnEnded;
}
UpdateText ();
UpdateFont ();
UpdateTextColor ();
UpdateKeyboard ();
UpdateEditable ();
UpdateTextAlignment ();
}
protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged (sender, e);
if (e.PropertyName == Editor.TextProperty.PropertyName)
UpdateText ();
else if (e.PropertyName == Xamarin.Forms.InputView.KeyboardProperty.PropertyName)
UpdateKeyboard ();
else if (e.PropertyName == VisualElement.IsEnabledProperty.PropertyName)
UpdateEditable ();
else if (e.PropertyName == Editor.TextColorProperty.PropertyName)
UpdateTextColor ();
else if (e.PropertyName == Editor.FontAttributesProperty.PropertyName)
UpdateFont ();
else if (e.PropertyName == Editor.FontFamilyProperty.PropertyName)
UpdateFont ();
else if (e.PropertyName == Editor.FontSizeProperty.PropertyName)
UpdateFont ();
}
void HandleChanged (object sender, EventArgs e)
{
ElementController.SetValueFromRenderer (Editor.TextProperty, Control.Text);
}
void OnEnded (object sender, EventArgs eventArgs)
{
if (Control.Text != Element.Text)
ElementController.SetValueFromRenderer (Editor.TextProperty, Control.Text);
Element.SetValue (VisualElement.IsFocusedPropertyKey, false);
ElementController.SendCompleted ();
}
void OnStarted (object sender, EventArgs eventArgs)
{
ElementController.SetValueFromRenderer (VisualElement.IsFocusedPropertyKey, true);
}
void UpdateEditable ()
{
Control.IsDisabled = !Element.IsEnabled;
}
void UpdateFont ()
{
Element.SetStyleFont (Element.FontFamily, Element.FontSize, Element.FontAttributes, Control.Style);
}
void UpdateKeyboard ()
{
}
void UpdateText ()
{
if (Control.Text != Element.Text)
Control.Text = Element.Text;
}
void UpdateTextAlignment ()
{
}
void UpdateTextColor ()
{
var textColor = Element.TextColor;
if (textColor.IsDefault)
Control.Style.Color = "black";
else
Control.Style.Color = textColor.ToOouiColor ();
}
}
}

View File

@ -18,11 +18,14 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="ButtonXamlPage.xaml.cs"> <Compile Update="ButtonXamlPage.xaml.cs">
<DependentUpon>*.xaml</DependentUpon> <DependentUpon>ButtonXamlPage.xaml</DependentUpon>
</Compile> </Compile>
<Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="DisplayAlertPage.xaml.cs"> <Compile Update="DisplayAlertPage.xaml.cs">
<DependentUpon>*.xaml</DependentUpon> <DependentUpon>DisplayAlertPage.xaml</DependentUpon>
</Compile>
<Compile Update="XamlPreviewPage.xaml.cs">
<DependentUpon>XamlPreviewPage.xaml</DependentUpon>
</Compile> </Compile>
</ItemGroup> </ItemGroup>
@ -33,6 +36,9 @@
<EmbeddedResource Update="DisplayAlertPage.xaml"> <EmbeddedResource Update="DisplayAlertPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator> <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Update="XamlPreviewPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup> </ItemGroup>
<PropertyGroup> <PropertyGroup>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Samples.XamlPreviewPage">
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Editor x:Name="editor" FontFamily="monospace" Grid.Row="0" Grid.Column="0" />
<ContentView x:Name="results" Grid.Row="0" Grid.Column="1" BackgroundColor="White" />
</Grid>
</ContentPage.Content>
</ContentPage>

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Samples
{
public partial class XamlPreviewPage : ContentPage
{
public XamlPreviewPage ()
{
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>
<Label Text=""Top Left"" Grid.Row=""0"" Grid.Column=""0"" />
<Label Text=""Top Right"" Grid.Row=""0"" Grid.Column=""1"" />
<Label Text=""Bottom Left"" Grid.Row=""1"" Grid.Column=""0"" />
<Label Text=""Bottom Right"" Grid.Row=""1"" Grid.Column=""1"" />
</Grid>
</ContentView>";
DisplayXaml ();
}
public void DisplayXaml ()
{
var asm = typeof (Xamarin.Forms.Xaml.Internals.XamlTypeResolver).Assembly;
var xamlLoaderType = asm.GetType ("Xamarin.Forms.Xaml.XamlLoader");
var loadArgTypes = new[] { typeof (object), typeof (string) };
var loadMethod = xamlLoaderType.GetMethod ("Load", System.Reflection.BindingFlags.Static|System.Reflection.BindingFlags.Public, null, System.Reflection.CallingConventions.Any, loadArgTypes, null);
var contentView = new ContentView ();
loadMethod.Invoke (null, new object[] { contentView, editor.Text });
results.Content = contentView;
}
}
}

View File

@ -0,0 +1,17 @@
using System;
using Xamarin.Forms;
namespace Samples
{
public class XamlPreviewPageSample : ISample
{
public string Title => "Xamarin.Forms XAML Editor";
public Ooui.Element CreateElement ()
{
var page = new XamlPreviewPage ();
return page.GetOouiElement ();
}
}
}