Implémentation de "User Dialogs" pour Windows phone 8, Windows 8 et Xamarin pour Windows
Voici un exemple d'implémentation du plugin UserDialogs pour la plateforme Windows.
L'auteur du plugin a indiqué qu'il ne supportera pas Windows 8. Il préfère se concentrer sur le développement du plugin pour Windows 10.
ConfirmAsync
public async Task<bool> ConfirmAsync(string message, string title = null, string okText = "OK", string cancelText = "Cancel") { var messgeDialog = new MessageDialog(message, title); messgeDialog.Commands.Add(new UICommand(okText)); messgeDialog.Commands.Add(new UICommand(cancelText)); messgeDialog.DefaultCommandIndex = 0; messgeDialog.CancelCommandIndex = 1; var result = await messgeDialog.ShowAsync(); if (result.Label.Equals(okText)) { return true; } else { return false; } }
AlertAsync
public async Task AlertAsync(string message, string title = null, string okText = "OK") { var md = new MessageDialog(message, title); await md.ShowAsync(); }
PromptAsync
public async TaskPromptAsync(string message, string title = null, string okText = "OK", string cancelText = "Cancel", string placeholder = "", bool secure = false) { var dialog = new PromptDialog(message, placeholder); var result = await dialog.ShowAsync(); if (result != null) { return result; } else { return new PromptResult() { Ok = false }; } }
PromptDialog.cs
using Acr.XamForms.UserDialogs; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading; using System.Threading.Tasks; using Windows.Foundation; using Windows.System; using Windows.UI; using Windows.UI.Core; using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; public class PromptDialog { private TaskCompletionSource_taskCompletionSource; private TextBlock _titleTextBlock; private TextBox _contentTextBlock; private Popup _popup; private bool _isPhone; public PromptDialog(string title, string placeHolderMessage, bool isPhone = false) { Title = title; PlaceHolderMessage = placeHolderMessage; _isPhone = isPhone; HeaderBrush = new SolidColorBrush(Colors.Black); } public string PlaceHolderMessage { get; set; } public Brush HeaderBrush { get; set; } public string Title { get; set; } public IAsyncOperation ShowAsync() { _popup = new Popup { Child = DrawPopup() }; if (_popup.Child != null) { SubscribeEvents(); _popup.IsOpen = true; } return AsyncInfo.Run(WaitForInput); } private Task WaitForInput(CancellationToken token) { _taskCompletionSource = new TaskCompletionSource (); token.Register(OnCanceled); return _taskCompletionSource.Task; } private Grid DrawPopup() { var content = Window.Current.Content as FrameworkElement; if (content == null) { // The dialog is being shown before content has been created for the window Window.Current.Activated += OnWindowActivated; return null; } double width = Window.Current.Bounds.Width; double height = Window.Current.Bounds.Height; var rootPanel = new Grid { Width = width, Height = height }; var overlay = new Grid { Background = new SolidColorBrush(Colors.Black), Opacity = 0.2D }; rootPanel.Children.Add(overlay); var dialog = new Grid { VerticalAlignment = VerticalAlignment.Center }; dialog.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) }); dialog.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); dialog.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) }); dialog.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); dialog.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) }); dialog.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); rootPanel.Children.Add(dialog); var titleBorder = new Border { Background = HeaderBrush, Height = 70 }; Grid.SetColumnSpan(titleBorder, 3); dialog.Children.Add(titleBorder); // Title _titleTextBlock = new TextBlock(); _titleTextBlock.Text = Title; _titleTextBlock.FontSize = _isPhone ? 16 : 24; _titleTextBlock.Foreground = new SolidColorBrush(Colors.White); _titleTextBlock.Margin = new Thickness(0, 0, 0, 20); _titleTextBlock.VerticalAlignment = VerticalAlignment.Bottom; Grid.SetColumn(_titleTextBlock, 1); dialog.Children.Add(_titleTextBlock); var infoBorder = new Border { Background = new SolidColorBrush(Colors.White) }; Grid.SetRow(infoBorder, 1); Grid.SetColumnSpan(infoBorder, 3); Grid.SetRowSpan(infoBorder, 2); dialog.Children.Add(infoBorder); var grid = new Grid { MinHeight = 120 }; Grid.SetRow(grid, 1); Grid.SetColumn(grid, 1); dialog.Children.Add(grid); StackPanel informationPanel = new StackPanel(); informationPanel.Margin = new Thickness(0, 20, 0, 30); // informationPanel.Width = 456D; Grid.SetColumn(informationPanel, 1); Grid.SetRow(informationPanel, 1); // Content Textbox _contentTextBlock = new TextBox(); _contentTextBlock.Text = PlaceHolderMessage; _contentTextBlock.BorderBrush = new SolidColorBrush(Colors.Black); _contentTextBlock.Margin = new Thickness(0, 20, 0, 4); informationPanel.Children.Add(_contentTextBlock); grid.Children.Add(informationPanel); // Buttons StackPanel stButtons = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right, Margin = new Thickness(0, 0, 0, 20) }; Button cancelButton = new Button(); cancelButton.BorderThickness = new Thickness(); cancelButton.Padding = new Thickness(10, 5, 10, 5); cancelButton.Content = "Cancel"; cancelButton.Foreground = new SolidColorBrush(Colors.White); cancelButton.Background = new SolidColorBrush(Colors.DarkGray); cancelButton.MinWidth = 90D; cancelButton.Click += (cancelSender, cancelArgs) => OnCanceled(); stButtons.Children.Add(cancelButton); Button okButton = new Button(); okButton.BorderThickness = new Thickness(); okButton.Padding = new Thickness(10, 5, 10, 5); okButton.Content = "OK"; okButton.Foreground = new SolidColorBrush(Colors.White); okButton.Background = new SolidColorBrush(Colors.Black); okButton.MinWidth = 90D; okButton.Click += (okSender, okArgs) => OnCompleted(); stButtons.Children.Add(okButton); Grid.SetColumn(stButtons, 1); Grid.SetRow(stButtons, 2); dialog.Children.Add(stButtons); return rootPanel; } private void PasswordTextBoxOnKeyUp(object sender, KeyRoutedEventArgs keyRoutedEventArgs) { if (keyRoutedEventArgs.Key == VirtualKey.Enter) { OnCompleted(); } } /// /// adjust for different view states /// /// /// private void OnWindowSizeChanged(object sender, WindowSizeChangedEventArgs e) { if (_popup.IsOpen == false) return; var child = _popup.Child as FrameworkElement; if (child == null) return; child.Width = e.Size.Width; child.Height = e.Size.Height; } private void OnInputHiding(InputPane sender, InputPaneVisibilityEventArgs args) { var child = _popup.Child as FrameworkElement; if (child == null) return; child.Margin = new Thickness(0); } private void OnKeyDown(object sender, KeyRoutedEventArgs e) { if (e.Key == VirtualKey.Escape) { OnCanceled(); } } private void OnWindowActivated(object sender, WindowActivatedEventArgs windowActivatedEventArgs) { Window.Current.Activated -= OnWindowActivated; SubscribeEvents(); _popup.Child = DrawPopup(); _popup.IsOpen = true; } private void OnCompleted() { UnsubscribeEvents(); var result = new PromptResult(); result.Ok = true; result.Text = _contentTextBlock.Text; _popup.IsOpen = false; _taskCompletionSource.SetResult(result); } private void OnCanceled() { UnsubscribeEvents(); // null to indicate dialog was canceled PromptResult result = null; _popup.IsOpen = false; _taskCompletionSource.SetResult(result); } private void SubscribeEvents() { Window.Current.SizeChanged += OnWindowSizeChanged; Window.Current.Content.KeyDown += OnKeyDown; var input = InputPane.GetForCurrentView(); input.Hiding += OnInputHiding; } private void UnsubscribeEvents() { Window.Current.SizeChanged -= OnWindowSizeChanged; Window.Current.Content.KeyDown -= OnKeyDown; var input = InputPane.GetForCurrentView(); input.Hiding -= OnInputHiding; } } }