Skip to content

Instantly share code, notes, and snippets.

@ertugrulozcan
Created December 7, 2016 22:03
Show Gist options
  • Save ertugrulozcan/efd9e82e3c0cd5b99c5a8040ef5578ed to your computer and use it in GitHub Desktop.
Save ertugrulozcan/efd9e82e3c0cd5b99c5a8040ef5578ed to your computer and use it in GitHub Desktop.
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Threading;
using WpfDialogManagement.Contracts;
using WpfDialogManagement.Manager;
using WpfDialogManagement.Views;
namespace WpfDialogManagement.Dialogs
{
abstract class DialogBase : IDialog, INotifyPropertyChanged
{
private bool isCloseButtonVisible;
protected DialogBase(IDialogHost dialogHost, DialogMode dialogMode, Dispatcher dispatcher, bool isCloseButtonVisible = true)
{
_dialogHost = dialogHost;
_dispatcher = dispatcher;
Mode = dialogMode;
CloseBehavior = DialogCloseBehavior.AutoCloseOnButtonClick;
this.isCloseButtonVisible = isCloseButtonVisible;
OkText = "Ok";
CancelText = "Cancel";
YesText = "Yes";
NoText = "No";
switch (dialogMode)
{
case DialogMode.None:
break;
case DialogMode.Ok:
CanOk = true;
break;
case DialogMode.Cancel:
CanCancel = true;
break;
case DialogMode.OkCancel:
CanOk = true;
CanCancel = true;
break;
case DialogMode.YesNo:
CanYes = true;
CanNo = true;
break;
case DialogMode.YesNoCancel:
CanYes = true;
CanNo = true;
CanCancel = true;
break;
default:
throw new ArgumentOutOfRangeException("dialogMode");
}
}
private readonly IDialogHost _dialogHost;
private readonly Dispatcher _dispatcher;
private object _content;
public object Content { get { return _content; } set { _content = value; } }
protected DialogBaseControl DialogBaseControl { get; private set; }
protected void SetContent(object content)
{
Content = content;
}
protected void InvokeUICall(Action del)
{
_dispatcher.Invoke(del, DispatcherPriority.DataBind);
}
#region Implementation of IDialog
public DialogMode Mode { get; private set; }
public DialogResultState Result { get; set; }
public DialogCloseBehavior CloseBehavior { get; set; }
public Action Ok { get; set; }
public Action Cancel { get; set; }
public Action Yes { get; set; }
public Action No { get; set; }
private bool _canOk;
public bool CanOk
{
get { return _canOk; }
set
{
_canOk = value;
OnPropertyChanged("CanOk");
}
}
private bool _canCancel;
public bool CanCancel
{
get { return _canCancel; }
set
{
_canCancel = value;
OnPropertyChanged("CanCancel");
}
}
private bool _canYes;
public bool CanYes
{
get { return _canYes; }
set
{
_canYes = value;
OnPropertyChanged("CanYes");
}
}
private bool _canNo;
public bool CanNo
{
get { return _canNo; }
set
{
_canNo = value;
OnPropertyChanged("CanNo");
}
}
public string OkText { get; set; }
public string CancelText { get; set; }
public string YesText { get; set; }
public string NoText { get; set; }
public string Caption { get; set; }
private VerticalAlignment? _verticalDialogAlignment;
public VerticalAlignment VerticalDialogAlignment
{
set
{
if (DialogBaseControl == null)
_verticalDialogAlignment = value;
else
DialogBaseControl.VerticalDialogAlignment = value;
}
}
private HorizontalAlignment? _horizontalDialogAlignment;
public HorizontalAlignment HorizontalDialogAlignment
{
set
{
if (DialogBaseControl == null)
_horizontalDialogAlignment = value;
else
DialogBaseControl.HorizontalDialogAlignment = value;
}
}
public void Show()
{
if (DialogBaseControl != null)
throw new Exception("The dialog can only be shown once.");
InvokeUICall(() =>
{
var originalContent = _dialogHost.GetCurrentContent();
var originalRegion = _dialogHost.GetCurrentRegion();
DialogBaseControl = new DialogBaseControl(originalContent, this, this.isCloseButtonVisible);
originalRegion.Visibility = Visibility.Collapsed;
DialogBaseControl.SetCustomContent(_content);
if (_verticalDialogAlignment.HasValue)
DialogBaseControl.VerticalDialogAlignment = _verticalDialogAlignment.Value;
if (_horizontalDialogAlignment.HasValue)
DialogBaseControl.HorizontalDialogAlignment = _horizontalDialogAlignment.Value;
_dialogHost.ShowDialog(DialogBaseControl);
});
}
public void Close()
{
// Dialog wird angezeigt?
if (DialogBaseControl == null)
return;
// Callbacks abhängen
Ok = null;
Cancel = null;
Yes = null;
No = null;
InvokeUICall(
() =>
{
_dialogHost.HideDialog(DialogBaseControl);
DialogBaseControl.SetCustomContent(null);
});
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using WpfDialogManagement.Contracts;
using WpfDialogManagement.Views;
namespace WpfDialogManagement.Helpers
{
class DialogLayeringHelper : IDialogHost
{
public DialogLayeringHelper(ContentControl parent, ContentControl layer, ContentControl region)
{
this._parent = parent;
this.layer = layer;
this.region = region;
}
private readonly ContentControl _parent;
private readonly ContentControl layer;
private readonly ContentControl region;
private readonly List<object> _layerStack = new List<object>();
public bool HasDialogLayers { get { return _layerStack.Any(); } }
#region Implementation of IDialogHost
public void ShowDialog(DialogBaseControl dialog)
{
_layerStack.Add(this.layer.Content);
this.layer.Content = dialog;
}
public void HideDialog(DialogBaseControl dialog)
{
if (this.layer.Content == dialog)
{
var oldContent = _layerStack.Last();
_layerStack.Remove(oldContent);
this.layer.Content = oldContent;
}
else
_layerStack.Remove(dialog);
this.region.Visibility = Visibility.Visible;
}
public FrameworkElement GetCurrentContent()
{
return this._parent;
}
public FrameworkElement GetCurrentRegion()
{
return this.region;
}
#endregion
}
}
using System;
using System.Windows.Controls;
using System.Windows.Threading;
using WpfDialogManagement.Contracts;
using WpfDialogManagement.Dialogs;
using WpfDialogManagement.Helpers;
namespace WpfDialogManagement.Manager
{
public class DialogManager : IDialogManager
{
public DialogManager(ContentControl parent, ContentControl layer, ContentControl region, Dispatcher dispatcher)
{
_dispatcher = dispatcher;
_dialogHost = new DialogLayeringHelper(parent, layer, region);
}
private readonly Dispatcher _dispatcher;
private readonly IDialogHost _dialogHost;
#region Implementation of IDialogManager
public IMessageDialog CreateMessageDialog(string message, DialogMode dialogMode)
{
IMessageDialog dialog = null;
dialog = new MessageDialog(_dialogHost, dialogMode, message, _dispatcher);
return dialog;
}
public IMessageDialog CreateMessageDialog(string message, string caption, DialogMode dialogMode)
{
IMessageDialog dialog = null;
dialog = new MessageDialog(_dialogHost, dialogMode, message, _dispatcher)
{
Caption = caption
};
return dialog;
}
public IProgressDialog CreateProgressDialog(DialogMode dialogMode)
{
IProgressDialog dialog = null;
InvokeInUIThread(() =>
{
dialog = WaitProgressDialog.CreateProgressDialog(_dialogHost, dialogMode, _dispatcher);
dialog.CloseWhenWorkerFinished = true;
});
return dialog;
}
public IProgressDialog CreateProgressDialog(string message, DialogMode dialogMode)
{
IProgressDialog dialog = null;
InvokeInUIThread(() =>
{
dialog = WaitProgressDialog.CreateProgressDialog(_dialogHost, dialogMode, _dispatcher);
dialog.CloseWhenWorkerFinished = true;
dialog.Message = message;
});
return dialog;
}
public IProgressDialog CreateProgressDialog(string message, string readyMessage, DialogMode dialogMode)
{
IProgressDialog dialog = null;
InvokeInUIThread(() =>
{
dialog = WaitProgressDialog.CreateProgressDialog(_dialogHost, dialogMode, _dispatcher);
dialog.CloseWhenWorkerFinished = false;
dialog.ReadyMessage = readyMessage;
dialog.Message = message;
});
return dialog;
}
public IWaitDialog CreateWaitDialog(DialogMode dialogMode)
{
IWaitDialog dialog = null;
InvokeInUIThread(() =>
{
dialog = WaitProgressDialog.CreateWaitDialog(_dialogHost, dialogMode, _dispatcher);
dialog.CloseWhenWorkerFinished = true;
});
return dialog;
}
public IWaitDialog CreateWaitDialog(string message, DialogMode dialogMode)
{
IWaitDialog dialog = null;
InvokeInUIThread(() =>
{
dialog = WaitProgressDialog.CreateWaitDialog(_dialogHost, dialogMode, _dispatcher);
dialog.CloseWhenWorkerFinished = true;
dialog.Message = message;
});
return dialog;
}
public IWaitDialog CreateWaitDialog(string message, string readyMessage, DialogMode dialogMode)
{
IWaitDialog dialog = null;
InvokeInUIThread(() =>
{
dialog = WaitProgressDialog.CreateWaitDialog(_dialogHost, dialogMode, _dispatcher);
dialog.CloseWhenWorkerFinished = false;
dialog.Message = message;
dialog.ReadyMessage = readyMessage;
});
return dialog;
}
public ICustomContentDialog CreateCustomContentDialog(object content, DialogMode dialogMode)
{
ICustomContentDialog dialog = null;
InvokeInUIThread(() =>
{
dialog = new CustomContentDialog(_dialogHost, dialogMode, content, _dispatcher);
});
return dialog;
}
public ICustomContentDialog CreateCustomContentDialog(object content, string caption, DialogMode dialogMode)
{
ICustomContentDialog dialog = null;
InvokeInUIThread(() =>
{
dialog = new CustomContentDialog(_dialogHost, dialogMode, content, _dispatcher)
{
Caption = caption
};
});
return dialog;
}
#endregion
private void InvokeInUIThread(Action del)
{
_dispatcher.Invoke(del);
}
}
}
using System.Windows;
using WpfDialogManagement.Views;
namespace WpfDialogManagement.Contracts
{
interface IDialogHost
{
void ShowDialog(DialogBaseControl dialog);
void HideDialog(DialogBaseControl dialog);
FrameworkElement GetCurrentContent();
FrameworkElement GetCurrentRegion();
}
}
<!--MainRegion'a isim verildi-->
<ContentControl x:Name="MainRegionContentControl" cal:RegionManager.RegionName="MainRegion" WindowChrome.IsHitTestVisibleInChrome="True"/>
// Sadece bu metod degisti (hatta sadece DialogManager'ın constructor'u degisti)
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
this.ScreenManager = new WpfScreenManager(this);
this.ScreenManager.OnCurrentScreenChanged += (args) =>
{
eventAggregator.GetEvent<CurrentScreenChangedEvent>().Publish(args);
this.InvalidateVisual();
};
System.Windows.Rect r = this.ScreenManager.CurrentScreen.WorkingArea;
this.Width = r.Width;
this.Height = r.Height;
DialogManager dm = new DialogManager(this, this.DialogManagerPlaceHolder, this.MainRegionContentControl, this.Dispatcher);
WpfUIWindowDialogService dialogService = new WpfUIWindowDialogService(dm, eventAggregator);
this.container.RegisterInstance<IUIWindowDialogService>(dialogService);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment