Merge pull request #32 from ahoefling/master

Added DisplayAlert Support
This commit is contained in:
Frank A. Krueger 2017-11-18 15:23:57 -06:00 committed by GitHub
commit ecd9683a26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 209 additions and 33 deletions

114
Ooui.Forms/DisplayAlert.cs Normal file
View File

@ -0,0 +1,114 @@
using System.Web;
using Xamarin.Forms.Internals;
namespace Ooui.Forms
{
public class DisplayAlert
{
private readonly Button _closeButton;
private readonly Button _acceptButton;
private readonly Button _cancelButton;
public DisplayAlert(AlertArguments arguments)
{
Element = new Div
{
ClassName = "modal-dialog"
};
var content = new Div
{
ClassName = "modal-content"
};
var header = new Div
{
ClassName = "modal-header"
};
_closeButton = new Button
{
ClassName = "close"
};
_closeButton.AppendChild(new Span(HttpUtility.HtmlDecode("×")));
var h4 = new Heading(4)
{
Text = arguments.Title
};
header.AppendChild(_closeButton);
header.AppendChild(h4);
content.AppendChild(header);
content.AppendChild(new Div()
{
ClassName = "modal-body",
Text = arguments.Message
});
if (!string.IsNullOrEmpty(arguments.Cancel))
{
var footer = new Div()
{
ClassName = "modal-footer"
};
_cancelButton = new Button(arguments.Cancel)
{
ClassName = "btn btn-default"
};
_cancelButton.Clicked += (s, e) => SetResult(false);
footer.AppendChild(_cancelButton);
if (!string.IsNullOrEmpty(arguments.Accept))
{
_acceptButton = new Button(arguments.Accept)
{
ClassName = "btn btn-default"
};
_acceptButton.Clicked += (s, e) => SetResult(true);
footer.AppendChild(_acceptButton);
}
content.AppendChild(footer);
}
Element.AppendChild(content);
void SetResult(bool result)
{
arguments.SetResult(result);
}
}
public event TargetEventHandler Clicked
{
add
{
_closeButton.Clicked += value;
if(_cancelButton != null)
_cancelButton.Clicked += value;
if(_acceptButton != null)
_acceptButton.Clicked += value;
}
remove
{
_closeButton.Clicked -= value;
if (_cancelButton != null)
_cancelButton.Clicked -= value;
if (_acceptButton != null)
_acceptButton.Clicked -= value;
}
}
public Element Element { get; private set; }
}
}

View File

@ -15,7 +15,6 @@
<PackageReference Include="Xamarin.Forms" Version="2.4.0.38779" />
</ItemGroup>
<ItemGroup>
<Folder Include="Renderers\" />
<Folder Include="Extensions\" />
</ItemGroup>
<ItemGroup>

View File

@ -1,11 +1,12 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Ooui.Forms.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using System.Web;
namespace Ooui.Forms
namespace Ooui.Forms
{
public class Platform : BindableObject, IPlatform, INavigation, IDisposable
{
@ -31,6 +32,19 @@ namespace Ooui.Forms
public Platform ()
{
_renderer = new PlatformRenderer (this);
MessagingCenter.Subscribe(this, Page.AlertSignalName, (Page sender, AlertArguments arguments) =>
{
var alert = new DisplayAlert(arguments);
alert.Clicked += CloseAlert;
_renderer.AppendChild(alert.Element);
void CloseAlert(object s, EventArgs e)
{
_renderer.RemoveChild(alert.Element);
}
});
}
void IDisposable.Dispose ()
@ -70,10 +84,10 @@ namespace Ooui.Forms
public SizeRequest GetNativeSize (VisualElement view, double widthConstraint, double heightConstraint)
{
var renderView = GetRenderer (view);
if (renderView == null || renderView.NativeView == null)
return new SizeRequest (Size.Zero);
var renderView = GetRenderer (view);
if (renderView == null || renderView.NativeView == null)
return new SizeRequest (Size.Zero);
return renderView.GetDesiredSize (widthConstraint, heightConstraint);
}
@ -173,5 +187,5 @@ namespace Ooui.Forms
{
throw new NotImplementedException ();
}
}
}
}
}

View File

@ -2,12 +2,12 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ButtonXaml.ButtonXamlPage">
<StackLayout Padding="20">
<Label Text="Welcome to Xamarin.Forms!"/>
<StackLayout Padding="20">
<Label Text="Welcome to Xamarin.Forms!"/>
<Label x:Name="LabelCount" Text="Click Count: 0"/>
<Button Text="Tap for click count!"
<Button Text="Tap for click count!"
HeightRequest="30"
Clicked="OnButtonClicked" />
</StackLayout>
</ContentPage>
</ContentPage>

View File

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

View File

@ -0,0 +1,13 @@
<?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.DisplayAlertSample">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to DisplayAlert Sample!" />
<Button Text="Tap for Display Alert"
HeightRequest="30"
Clicked="OnButtonClicked" />
</StackLayout>
</ContentPage.Content>
</ContentPage>

View File

@ -0,0 +1,29 @@
using Ooui;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Samples
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DisplayAlertSample : ContentPage
{
public DisplayAlertSample ()
{
InitializeComponent ();
}
public async void OnButtonClicked(object sender, EventArgs args)
{
var result = await DisplayAlert("Alert Message", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa.", "YES", "NO");
await DisplayAlert("Alert Response", $"You selected value: {result}", "OK");
}
public void Publish()
{
var sample = new XamlPageSample();
UI.Publish("/display-alert", sample.CreateElement(this));
}
}
}

View File

@ -28,9 +28,10 @@ namespace Samples
new ButtonSample ().Publish ();
new TodoSample ().Publish ();
new DrawSample ().Publish ();
new FilesSample ().Publish ();
new FilesSample ().Publish ();
new DisplayAlertSample ().Publish ();
UI.Present ("/todo");
UI.Present ("/display-alert");
Console.ReadLine ();
}

View File

@ -17,10 +17,19 @@
<EmbeddedResource Include="**/*.xaml" />
</ItemGroup>
<ItemGroup>
<Compile Update="DisplayAlertSample.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="ButtonXamlPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="DisplayAlertSample.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<PropertyGroup>

14
Samples/XamlPageSample.cs Normal file
View File

@ -0,0 +1,14 @@
using Xamarin.Forms;
namespace Samples
{
public class XamlPageSample
{
public string Title => "Xamarin.Forms Button XAML";
public Ooui.Element CreateElement (Page page)
{
return page.GetOouiElement ();
}
}
}