Add PCL target to Xamarin.Forms

This commit is contained in:
Frank A. Krueger 2018-03-09 21:28:39 -08:00
parent 03781339f4
commit 0f085717ad
No known key found for this signature in database
GPG Key ID: 0471C67474FFE664
10 changed files with 143 additions and 37 deletions

View File

@ -1,4 +1,4 @@
using System.Web;
using System;
using Xamarin.Forms.Internals;
namespace Ooui.Forms
@ -31,7 +31,7 @@ namespace Ooui.Forms
ClassName = "close"
};
_closeButton.AppendChild(new Span(HttpUtility.HtmlDecode("×")));
_closeButton.AppendChild(new Span("×"));
var h4 = new Heading(4)
{

View File

@ -21,7 +21,7 @@ namespace Xamarin.Forms
return;
IsInitialized = true;
Log.Listeners.Add (new DelegateLogListener ((c, m) => Trace.WriteLine (m, c)));
Log.Listeners.Add (new DelegateLogListener ((c, m) => System.Diagnostics.Debug.WriteLine (m, c)));
Device.SetIdiom (TargetIdiom.Desktop);
Device.PlatformServices = new OouiPlatformServices ();
@ -64,7 +64,11 @@ namespace Xamarin.Forms
public Assembly[] GetAssemblies ()
{
#if PCL
return new[] { typeof (Xamarin.Forms.View).GetTypeInfo ().Assembly, typeof (Forms.OouiPlatformServices).GetTypeInfo ().Assembly };
#else
return AppDomain.CurrentDomain.GetAssemblies ();
#endif
}
public string GetMD5Hash (string input)
@ -140,6 +144,31 @@ namespace Xamarin.Forms
}
}
#if PCL
public delegate void TimerCallback(object state);
public sealed class Timer : CancellationTokenSource, IDisposable
{
public Timer (TimerCallback callback, object state, int dueTime, int period)
{
Task.Run (async () => {
await Task.Delay (dueTime).ConfigureAwait (false);
if (!IsCancellationRequested)
callback (state);
while (!IsCancellationRequested) {
await Task.Delay (period).ConfigureAwait (false);
if (!IsCancellationRequested)
callback (state);
}
});
}
public new void Dispose() { base.Cancel(); }
}
#endif
public void QuitApplication()
{
}

View File

@ -9,16 +9,14 @@
<PackageProjectUrl>https://github.com/praeclarum/Ooui</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/praeclarum/Ooui/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/praeclarum/Ooui.git</RepositoryUrl>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard1.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType></DebugType>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<PropertyGroup Condition=" $(TargetFramework) == 'netstandard1.0' ">
<DefineConstants>PCL</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="2.5.0.122203" />
</ItemGroup>

View File

@ -5,6 +5,7 @@ namespace Xamarin.Forms
{
public static class PageExtensions
{
#if !PCL
public static void Publish (this Xamarin.Forms.Page page, string path)
{
Ooui.UI.Publish (path, () => page.CreateElement ());
@ -15,6 +16,7 @@ namespace Xamarin.Forms
var lazyPage = new Lazy<Ooui.Element> ((() => page.CreateElement ()), true);
Ooui.UI.Publish (path, () => lazyPage.Value);
}
#endif
public static Ooui.Element GetOouiElement (this Xamarin.Forms.Page page)
{

View File

@ -4,7 +4,6 @@ using System.Threading.Tasks;
using Ooui.Forms.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using System.Web;
namespace Ooui.Forms
{
@ -142,7 +141,7 @@ namespace Ooui.Forms
void AddChild (VisualElement view)
{
if (!Application.IsApplicationOrNull (view.RealParent))
Console.Error.WriteLine ("Tried to add parented view to canvas directly");
System.Diagnostics.Debug.WriteLine ("Tried to add parented view to canvas directly");
if (GetRenderer (view) == null) {
var viewRenderer = CreateRenderer (view);
@ -152,7 +151,7 @@ namespace Ooui.Forms
viewRenderer.SetElementSize (new Size (640, 480));
}
else
Console.Error.WriteLine ("Potential view double add");
System.Diagnostics.Debug.WriteLine ("Potential view double add");
}
void HandleRendererStyle_PropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)

View File

@ -139,8 +139,12 @@ namespace Ooui.Forms.Renderers
public sealed class FileImageSourceHandler : IImageSourceHandler
{
#pragma warning disable 1998
public async Task<string> LoadImageAsync (ImageSource imagesource, CancellationToken cancelationToken = default (CancellationToken), float scale = 1f)
{
#if PCL
return null;
#else
string image = null;
var filesource = imagesource as FileImageSource;
var file = filesource?.File;
@ -155,6 +159,7 @@ namespace Ooui.Forms.Renderers
}
}
return image;
#endif
}
}
@ -162,6 +167,9 @@ namespace Ooui.Forms.Renderers
{
public async Task<string> LoadImageAsync (ImageSource imagesource, CancellationToken cancelationToken = default (CancellationToken), float scale = 1f)
{
#if PCL
return null;
#else
string image = null;
var streamsource = imagesource as StreamImageSource;
if (streamsource?.Stream != null) {
@ -171,7 +179,7 @@ namespace Ooui.Forms.Renderers
using (var outputStream = new System.IO.MemoryStream (data)) {
await streamImage.CopyToAsync (outputStream, 4096, cancelationToken).ConfigureAwait (false);
}
var hash = Ooui.UI.Hash (data);
var hash = Ooui.Utilities.Hash (data);
var etag = "\"" + hash + "\"";
image = "/images/" + hash;
if (Ooui.UI.TryGetFileContentAtPath (image, out var file) && file.Etag == etag) {
@ -188,6 +196,7 @@ namespace Ooui.Forms.Renderers
System.Diagnostics.Debug.WriteLine ("Could not load image: {0}", streamsource);
}
return image;
#endif
}
}

View File

@ -40,3 +40,12 @@ namespace Ooui.Forms
}
}
}
#if PCL
namespace System.Collections.Concurrent
{
class ConcurrentDictionary<K, V> : Dictionary<K, V>
{
}
}
#endif

View File

@ -44,6 +44,9 @@
"System.Xml.dll",
"System.Xml.ReaderWriter.dll",
"System.Xml.XDocument.dll",
"Xamarin.Forms.Core.dll",
"Xamarin.Forms.Platform.dll",
"Xamarin.Forms.Xaml.dll",
"Ooui.dll",
mainAsmName + ".dll"
];

View File

@ -14,10 +14,8 @@ namespace Ooui
public const int MaxFps = 30;
#if !PCL
static readonly ManualResetEvent started = new ManualResetEvent (false);
[ThreadStatic]
static System.Security.Cryptography.SHA256 sha256;
static readonly ManualResetEvent started = new ManualResetEvent (false);
static CancellationTokenSource serverCts;
@ -82,22 +80,7 @@ namespace Ooui
clientJsBytes = Encoding.UTF8.GetBytes (r.ReadToEnd ());
}
}
clientJsEtag = "\"" + Hash (clientJsBytes) + "\"";
}
public static string Hash (byte[] bytes)
{
var sha = sha256;
if (sha == null) {
sha = System.Security.Cryptography.SHA256.Create ();
sha256 = sha;
}
var data = sha.ComputeHash (bytes);
StringBuilder sBuilder = new StringBuilder ();
for (int i = 0; i < data.Length; i++) {
sBuilder.Append (data[i].ToString ("x2"));
}
return sBuilder.ToString ();
clientJsEtag = "\"" + Utilities.Hash (clientJsBytes) + "\"";
}
static void Publish (string path, RequestHandler handler)
@ -129,13 +112,13 @@ namespace Ooui
if (contentType == null) {
contentType = GuessContentType (path, filePath);
}
var etag = "\"" + Hash (data) + "\"";
var etag = "\"" + Utilities.Hash (data) + "\"";
Publish (path, new DataHandler (data, etag, contentType));
}
public static void PublishFile (string path, byte[] data, string contentType)
{
var etag = "\"" + Hash (data) + "\"";
var etag = "\"" + Utilities.Hash (data) + "\"";
Publish (path, new DataHandler (data, etag, contentType));
}
@ -180,7 +163,7 @@ namespace Ooui
public static void PublishJson (string path, object value)
{
var data = JsonHandler.GetData (value);
var etag = "\"" + Hash (data) + "\"";
var etag = "\"" + Utilities.Hash (data) + "\"";
Publish (path, new DataHandler (data, etag, JsonHandler.ContentType));
}
@ -379,13 +362,18 @@ namespace Ooui
}
}
static string EscapeHtml (string text)
{
return text.Replace ("&", "&amp;").Replace ("<", "&lt;");
}
public static void RenderTemplate (TextWriter writer, string webSocketPath, string title, string initialHtml)
{
writer.Write (@"<!DOCTYPE html>
<html>
<head>
<title>");
writer.Write (title.Replace ("&", "&amp;").Replace ("<", "&lt;"));
writer.Write (EscapeHtml (title));
writer.Write (@"</title>
<meta name=""viewport"" content=""width=device-width, initial-scale=1"" />
");

69
Ooui/Utilities.cs Normal file
View File

@ -0,0 +1,69 @@
using System;
using System.Text;
namespace Ooui
{
public static class Utilities
{
#if PCL
static readonly uint[] crcTable;
static Utilities ()
{
uint p = 0x04C11DB7;
crcTable = new uint[256];
for (uint c = 0; c <= 0xFF; c++) {
crcTable[c] = CrcReflect (c, 8) << 24;
for (uint i = 0; i < 8; i++) {
crcTable[c] = (crcTable[c] << 1) ^ (((crcTable[c] & (1u << 31)) != 0) ? p : 0);
}
crcTable[c] = CrcReflect (crcTable[c], 32);
}
}
static uint CrcReflect (uint r, byte c)
{
uint v = 0;
for (int i = 1; i < (c + 1); i++) {
if ((r & 1) != 0) {
v |= (1u << (c - i));
}
r >>= 1;
}
return v;
}
public static string Hash (byte[] bytes)
{
uint crc = 0xffffffffu;
for (var i = 0; i < bytes.Length; i++) {
crc = (crc >> 8) ^ crcTable[(crc & 0xff) ^ bytes[i]];
}
crc ^= 0xffffffffu;
return crc.ToString ("x8");
}
#else
[ThreadStatic]
static System.Security.Cryptography.SHA256 sha256;
public static string Hash (byte[] bytes)
{
var sha = sha256;
if (sha == null) {
sha = System.Security.Cryptography.SHA256.Create ();
sha256 = sha;
}
var data = sha.ComputeHash (bytes);
StringBuilder sBuilder = new StringBuilder ();
for (int i = 0; i < data.Length; i++) {
sBuilder.Append (data[i].ToString ("x2"));
}
return sBuilder.ToString ();
}
#endif
}
}