Merge pull request #32 from ahoefling/master
Added DisplayAlert Support
This commit is contained in:
commit
ecd9683a26
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -15,7 +15,6 @@
|
|||
<PackageReference Include="Xamarin.Forms" Version="2.4.0.38779" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Renderers\" />
|
||||
<Folder Include="Extensions\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Threading.Tasks;
|
|||
using Ooui.Forms.Renderers;
|
||||
using Xamarin.Forms;
|
||||
using Xamarin.Forms.Internals;
|
||||
using System.Web;
|
||||
|
||||
namespace Ooui.Forms
|
||||
{
|
||||
|
@ -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 ()
|
||||
|
|
|
@ -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 ();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,8 +29,9 @@ namespace Samples
|
|||
new TodoSample ().Publish ();
|
||||
new DrawSample ().Publish ();
|
||||
new FilesSample ().Publish ();
|
||||
new DisplayAlertSample ().Publish ();
|
||||
|
||||
UI.Present ("/todo");
|
||||
UI.Present ("/display-alert");
|
||||
|
||||
Console.ReadLine ();
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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 ();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue