Skip to content

Instantly share code, notes, and snippets.

@cureos
Last active December 20, 2015 01:59
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 cureos/6053471 to your computer and use it in GitHub Desktop.
Save cureos/6053471 to your computer and use it in GitHub Desktop.
WPF specific view presenter class supporting closing an MVVMCross based application
public class MvxCloseableWpfViewPresenter : MvxWpfViewPresenter
{
#region FIELDS
private readonly Window _window;
#endregion
#region CONSTRUCTORS
public MvxCloseableWpfViewPresenter(Window window)
{
if (window == null) throw new ArgumentNullException("window");
_window = window;
}
#endregion
#region METHODS
public override void Present(FrameworkElement frameworkElement)
{
_window.Content = frameworkElement;
}
public override void ChangePresentation(MvxPresentationHint hint)
{
IMvxWpfView view;
var closeHint = hint as MvxClosePresentationHint;
if (closeHint != null && (view = _window.Content as IMvxWpfView) != null &&
ReferenceEquals(closeHint.ViewModelToClose, view.ViewModel))
_window.Close();
base.ChangePresentation(hint);
}
#endregion
}
@heyixiaoran
Copy link

Hi Sir.
I used your code. Now I can show sub window and close it.But there is a little different in ChangePresentation method.Maybe it's not good.I hope can get your guide.
Thanks.
public class MvxWindowViewPresenter : MvxWpfViewPresenter
{
private readonly ContentControl _contentControl;

    private FrameworkElement _frameworkElement;
    public MvxWindowViewPresenter(ContentControl contentControl)
    {
        _contentControl = contentControl;
    }

    public override void Present(FrameworkElement frameworkElement)
    {
        _frameworkElement = frameworkElement;
        var window = frameworkElement as Window;
        if (window == null)
        {
            _contentControl.Content = frameworkElement;
        }
        else
        {
            window.Owner = _contentControl as Window;
            window.ShowDialog();
        }
    }

    public override void ChangePresentation(MvxPresentationHint hint)
    {
        IMvxWpfView view;
        var closeHint = hint as MvxClosePresentationHint;
        if (closeHint != null
            && (view = _frameworkElement as IMvxWpfView) != null
            && ReferenceEquals(closeHint.ViewModelToClose, view.ViewModel))
        {
            (_frameworkElement as Window).Close();
        }

        base.ChangePresentation(hint);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment