Skip to content

Instantly share code, notes, and snippets.

@tkouba
Created November 26, 2020 16:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkouba/2dd84a513ccec5bf4329a595c2bdc759 to your computer and use it in GitHub Desktop.
Save tkouba/2dd84a513ccec5bf4329a595c2bdc759 to your computer and use it in GitHub Desktop.
Acr.UserDialog WPF implementation preview
namespace Acr.UserDialogs
{
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
using Acr.UserDialogs.Infrastructure;
using SAPXamarinTransporter.WPF.UserDialogs;
using Xamarin.Forms.Platform.WPF.Controls;
public class UserDialogsImplWPF : AbstractUserDialogs
{
// For not implemented features but not throw exception
private class DummyDisposable : IDisposable
{
void IDisposable.Dispose() { }
}
private class ProgressDialogImpl : IProgressDialog
{
private readonly ProgressDialogConfig config;
private readonly System.Windows.Controls.ProgressBar progressBar;
private readonly FormsContentDialog dialog;
public string Title
{
get => dialog.Title as string;
set => dialog.Title = value;
}
public int PercentComplete
{
get => Convert.ToInt32(progressBar.Value);
set => progressBar.Value = Math.Max(progressBar.Minimum, Math.Min(progressBar.Maximum, value));
}
public bool IsShowing { get; private set; }
public ProgressDialogImpl(ProgressDialogConfig config)
{
this.config = config;
this.progressBar = new System.Windows.Controls.ProgressBar()
{
IsIndeterminate = !config.IsDeterministic,
Minimum = 0,
Maximum = 100,
Height = 40
};
this.dialog = new FormsContentDialog()
{
Title = config.Title,
Content = progressBar
};
}
public void Dispose()
{
HideContentDialog();
}
public void Hide()
{
HideContentDialog();
IsShowing = false;
}
public void Show()
{
ShowContentDialog(this.dialog);
IsShowing = true;
}
}
public UserDialogsImplWPF()
{
}
public override IDisposable ActionSheet(ActionSheetConfig config)
{
return new DummyDisposable();
}
public override IDisposable Alert(AlertConfig config)
{
FormsContentDialog dialog = new FormsContentDialog()
{
Title = config.Title,
Content = config.Message,
IsPrimaryButtonEnabled = true,
PrimaryButtonText = config.OkText
};
dialog.PrimaryButtonClick += (s, e) => { HideContentDialog(); e.Cancel = true; };
ShowContentDialog(dialog);
return new DisposableAction(HideContentDialog);
}
public override async Task AlertAsync(AlertConfig config, CancellationToken? cancelToken = null)
{
FormsContentDialog dialog = new FormsContentDialog()
{
Title = config.Title,
Content = config.Message,
IsPrimaryButtonEnabled = true,
PrimaryButtonText = config.OkText
};
await dialog.ShowAsync();
}
public override IDisposable Confirm(ConfirmConfig config)
{
FormsContentDialog dialog = new FormsContentDialog()
{
Title = config.Title,
Content = config.Message,
IsPrimaryButtonEnabled = true,
PrimaryButtonText = config.OkText,
IsSecondaryButtonEnabled = true,
SecondaryButtonText = config.CancelText
};
dialog.PrimaryButtonClick += (s, e) => { HideContentDialog(); config.OnAction(true); e.Cancel = true; };
dialog.SecondaryButtonClick += (s, e) => { HideContentDialog(); config.OnAction(false); e.Cancel = true; };
ShowContentDialog(dialog);
return new DisposableAction(HideContentDialog);
}
public override async Task<bool> ConfirmAsync(ConfirmConfig config, CancellationToken? cancelToken = null)
{
FormsContentDialog dialog = new FormsContentDialog()
{
Title = config.Title,
Content = config.Message,
IsPrimaryButtonEnabled = true,
PrimaryButtonText = config.OkText,
IsSecondaryButtonEnabled = true,
SecondaryButtonText = config.CancelText
};
LightContentDialogResult result = await dialog.ShowAsync();
return result == LightContentDialogResult.Primary;
}
public override IDisposable DatePrompt(DatePromptConfig config)
{
return new DummyDisposable();
}
public override IDisposable Login(LoginConfig config)
{
throw new NotImplementedException();
}
public override IDisposable Prompt(PromptConfig config)
{
return new DummyDisposable();
}
public override IDisposable TimePrompt(TimePromptConfig config)
{
return new DummyDisposable();
}
public override IDisposable Toast(ToastConfig config)
{
return new DummyDisposable();
}
protected override IProgressDialog CreateDialogInstance(ProgressDialogConfig config)
{
return new ProgressDialogImpl(config);
}
private static void ShowContentDialog(FormsContentDialog dialog)
{
if (System.Windows.Application.Current?.MainWindow is FormsWindow window)
{
window.ShowContentDialog(dialog);
}
}
private static void HideContentDialog()
{
try
{
if (System.Windows.Application.Current?.MainWindow is FormsWindow window)
{
window.HideContentDialog();
}
}
catch (Exception ex)
{
Log.Error("Dismiss", $"Error hide content dialog - {ex.Message}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment