Chapter.Net.WPF.Navigation 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Chapter.Net.WPF.Navigation --version 1.2.0                
NuGet\Install-Package Chapter.Net.WPF.Navigation -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Chapter.Net.WPF.Navigation" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Chapter.Net.WPF.Navigation --version 1.2.0                
#r "nuget: Chapter.Net.WPF.Navigation, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Chapter.Net.WPF.Navigation as a Cake Addin
#addin nuget:?package=Chapter.Net.WPF.Navigation&version=1.2.0

// Install Chapter.Net.WPF.Navigation as a Cake Tool
#tool nuget:?package=Chapter.Net.WPF.Navigation&version=1.2.0                

Chapter.Net.WPF.Navigation Library

Overview

Chapter.Net.WPF.Navigation brings everything you need to open windows, close windows, use system dialogs, show messages boxes and more till an 'async constructor'. Its the perfect base and very easy to use navigation framework for any kind of application, multi or single window applications.

Features

  • INavigationService: The main interface which brings all together. Display dialogs, windows, user controls, calls the ILoader and IEditable. The single point of entry to do anything in regard of navigation.
  • Use alltogether or stand alone: Objects like the IDialogProvider or IMessageBoxProvider or IWindowProvider can be used stand alone even when give into a INavigationService.
  • Browse for files and folders: Using the IDialogProvider any system dialog, browse folder, save file, open file, can be used out of ViewModels.
  • Show message boxes: Using the IMessageBoxProvider any kind of message boxes can be used out of ViewModels.
  • Maintain windows: Using the IWindowProvider you can create new and get existing windows (and user controls).
  • Host single user controls: Using the SingleNavigationPresenter out of ViewModels UserControls can be displayed and replaced on the UI.
  • Host stacked user controls: Using the StackedNavigationPresenter out of ViewModels UserControls can be added and removed on the UI.
  • Async ctor: Using the INavigationService each ViewModel can implement the ILoader to have an async constructor in the ViewModel, called each time its UI element gets shown.
  • Leave prevention: Using the INavigationService each ViewModel can implement the IEditable to get informed if the user tries to close a window and it can be prevented like user have to save changes first and stuff like that.

Getting Started

  1. Installation:

    • Install the Chapter.Net.WPF.Navigation library via NuGet Package Manager:
    dotnet add package Chapter.Net.WPF.Navigation
    
  2. INavigationService:

    public void Bootstrapper
    {
        IUnityContainer _unityContainer;
    
        public Bootstrapper()
        {
            _unityContainer = new UnityContainer();
            _unityContainer.RegisterSingleton<IWindowProvider, WindowProvider>();
            _unityContainer.RegisterType<IDialogProvider, DialogProvider>();
            _unityContainer.RegisterType<IMessageBoxProvider, MessageBoxProvider>();
            _unityContainer.RegisterType<INavigationService, NavigationService>();
    
            RegisterViews();
        }
    
        public void RegisterViews()
        {
            var windowProvider = (WindowProvider)_unityContainer.Resolve<IWindowProvider>();
    
            windowProvider.RegisterWindow<MainView>("MainView");
            windowProvider.RegisterWindow<SubView>("SubView");
    
            windowProvider.RegisterControl<DialogsView>("DialogsView");
            windowProvider.RegisterControl<DisplayControlView>("DisplayControlView");
        }
    }
    
    public class WindowViewModel : ObservableObject, ILoader
    {
        public async Task Load()
        {
            // Loads the data as soon the window got shown.
            await Task.CompletedTask;
        }
    }
    
    public void ViewModel : ObservableObject
    {
        private INavigationService _navigationService;
    
        public ViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
        }
    
        public async Task ShowWindow()
        {
            var vm = new WindowViewModel();
            await _navigationService.ShowModalWindow("MainView", vm);
        }
    }
    
    [TestFixture]
    public class ViewModelTests
    {
        private Mock<INavigationService> _navigationService;
        private ViewModel _target;
    
        [SetUp]
        public void Setup()
        {
            _navigationService = new Mock<INavigationService>();
            _target = new ViewModel(_navigationService.Object);
        }
    
        [Test]
        public void ShowWindow_Called_ShowsTheWindow()
        {
            _target.ShowWindow();
    
            _navigationService.Verify(x => x.ShowModalWindow(Args.Any<string>(), Args.Any<object>()), Times.Once);
        }
    }
    
  3. Browse for files and folders:

    public class ViewModel : ObservableObject
    {
        private IDialogProvider _dialogProvider;
    
        public ViewModel(IDialogProvider dialogProvider)
        {
            _dialogProvider = dialogProvider;
        }
    
        public string GetFile()
        {
            var data = new OpenFileData
            {
                CheckFileExists = true,
                MultiSelect = false
            };
    
            if (_dialogProvider.Show(data))
                return data.FileName;
    
            return null;
        }
    }
    
    [TestFixture]
    public class ViewModelTests
    {
        private Mock<IDialogProvider> _dialogProvider;
        private ViewModel _target;
    
        [SetUp]
        public void Setup()
        {
            _dialogProvider = new Mock<IDialogProvider>();
            _target = new ViewModel(_dialogProvider.Object);
        }
    
        [Test]
        public void GetFile_Called_ReturnsTheUserSelectedFile()
        {
            _dialogProvider.Setup(x => x.Show(Arg.Any<IOpenFileData>())
                .Callback(e => e.FileName = "filename")
                .Returns(true);
    
            var result = _target.GetFile();
    
            Assert.That(result, Is.EqualTo("filename"));
        }
    }
    
  4. Show message boxes:

    public class ViewModel : ObservableObject
    {
        private IMessageBoxProvider _messageBoxProvider;
    
        public ViewModel(IMessageBoxProvider messageBoxProvider)
        {
            _messageBoxProvider = messageBoxProvider;
        }
    
        public bool ShallDeleteFile()
        {
            var result = _messageBoxProvider.Show("Do you want to delete the file?", "Question", MessageBoxButton.YesNo);
            return result == MessageBoxResult.Yes;
        }
    }
    
    public class ViewModelTests
    {
        private Mock<IMessageBoxProvider> _messageBoxProvider;
        private ViewModel _target;
    
        [SetUp]
        public void Setup()
        {
            _messageBoxProvider = new Mock<IMessageBoxProvider>();
            _target = new ViewModel(_messageBoxProvider.Object);
        }
    
        [Test]
        public void ShallDeleteFile_UserConfirmed_ReturnsTrue()
        {
            _messageBoxProvider.Setup(x => x.Show(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<MessageBoxButton>())
                .Returns(MessageBoxResult.Yes);
    
            var result = _target.ShallDeleteFile();
    
            Assert.That(result, Is.True);
        }
    
        [Test]
        public void ShallDeleteFile_UserRejected_ReturnsTrue()
        {
            _messageBoxProvider.Setup(x => x.Show(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<MessageBoxButton>())
                .Returns(MessageBoxResult.No);
    
            var result = _target.ShallDeleteFile();
    
            Assert.That(result, Is.False);
        }
    }
    
  5. Maintain windows:

    public class Bootstrapper
    {
        public Bootstrapper()
        {
            _container = new UnityContainer();
            _container.RegisterSingleton<IWindowProvider, WindowProvider>();
        }
    
        public void Startup()
        {
            var provider = (WindowProvider)_container.Resolve<IWindowProvider>();
            provider.Register<MainView>("MainView");
        }
    }
    
    public class ViewModel : ObservableObject
    {
        private IWindowProvider _windowProvider;
    
        public ViewModel(IWindowProvider windowProvider)
        {
            _windowProvider = windowProvider;
        }
    
        public void Show(object viewModel)
        {
            var window = _windowProvider.GetNewWindow("MainView");
            window.DataContext = viewModel;
            window.ShowDialog();
        }
    }
    
  6. Host single user controls:

    <Window>
        <chapter:SingleNavigationPresenter ID="MainSpot" />
    </Window>
    
    public class ViewModel : ObservableObject
    {
        private INavigationService _navigationService;
    
        public ViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
        }
    
        public async Task SwitchAsync()
        {
            var vm = new ControlViewModel();
            // "Control" is the user control registered in the IWindowService for the INavigationService.
            await _navigationService.ShowControl("MainSpot", "Control", vm);
        }
    }
    
  7. Host stacked user controls:

    <Window>
        <chapter:StackedNavigationPresenter Strategy="Overlapping" ID="MainSpot" />
    </Window>
    
    public class ViewModel : ObservableObject
    {
        private INavigationService _navigationService;
    
        public ViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
        }
    
        public async Task SwitchAsync()
        {
            var vm = new ControlViewModel();
            // "Control" is the user control registered in the IWindowService for the INavigationService.
            await _navigationService.ShowControl("MainSpot", "Control", vm);
        }
    }
    
  8. Async ctor:

    public class WindowViewModel : ObservableObject, ILoader
    {
        public async Task Load()
        {
            // Loads the data as soon the window got shown.
            await Task.CompletedTask;
        }
    }
    
  9. Leave prevention:

    public class WindowViewModel : ObservableObject, IEditable
    {
        public async Task<bool> TryLeave()
        {
            // Check for data be saved.
            await Task.CompletedTask;
            return true;
        }
    }
    

License

Copyright (c) David Wendland. All rights reserved. Licensed under the MIT License. See LICENSE file in the project root for full license information.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net5.0-windows7.0 is compatible.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net6.0-windows7.0 is compatible.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net7.0-windows7.0 is compatible.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible. 
.NET Core netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 80 6/7/2024
1.2.0 156 3/31/2024
1.1.0 170 3/21/2024
1.0.0 553 12/25/2023