Skip to content

Commit

Permalink
added new API for registering navigation, added sample app for demosn…
Browse files Browse the repository at this point in the history
…trating page navigation
  • Loading branch information
suxrobGM committed Jun 28, 2021
1 parent 052827d commit 05ed703
Show file tree
Hide file tree
Showing 28 changed files with 555 additions and 100 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*.userosscache
*.sln.docstates

.idea/

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

Expand Down
2 changes: 1 addition & 1 deletion MagicMvvm.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 17.0.31410.414
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MagicMvvm.Core", "src\MagicMvvm.Core\MagicMvvm.Core.csproj", "{8F472812-A1BD-42BF-8716-A5C737FDDF18}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MagicMvvm.Wpf", "src\MagicMvvm.Wpf\MagicMvvm.Wpf.csproj", "{9881CE1C-9A65-4C25-92D5-FA048E59F59B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MagicMvvm.Wpf", "src\MagicMvvm.Wpf\MagicMvvm.Wpf.csproj", "{9881CE1C-9A65-4C25-92D5-FA048E59F59B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
25 changes: 25 additions & 0 deletions samples/03-AppNavigation/AppNavigation.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31423.177
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppNavigation", "AppNavigation\AppNavigation.csproj", "{A5862B3C-F8B0-4D31-81A7-48BD23B833E7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A5862B3C-F8B0-4D31-81A7-48BD23B833E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5862B3C-F8B0-4D31-81A7-48BD23B833E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5862B3C-F8B0-4D31-81A7-48BD23B833E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5862B3C-F8B0-4D31-81A7-48BD23B833E7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CB208487-14F8-4AE0-A457-B4099F017809}
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions samples/03-AppNavigation/AppNavigation/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Application x:Class="AppNavigation.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AppNavigation">
<Application.Resources>
</Application.Resources>
</Application>
78 changes: 78 additions & 0 deletions samples/03-AppNavigation/AppNavigation/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Windows;
using DryIoc;
using MagicMvvm.Navigation;

using AppNavigation.ViewModels;
using AppNavigation.ViewModels.Pages;
using AppNavigation.Views;
using AppNavigation.Views.Pages;

namespace AppNavigation
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
#region Public fields

// Readonly instance of DryIoc container.
public static readonly IContainer Container = new Container();

#endregion

#region Lifecycle methods

/// <summary>
/// Application entry point.
/// </summary>
/// <param name="e">Args</param>
protected override void OnStartup(StartupEventArgs e)
{
// Call lifecycle methods.
// Note: It's important to call RegisterTypes() method first of all.
RegisterTypes();
RegisterNavigation();

// Resolve MainWindow from container then display main window.
Container.Resolve<MainWindow>().Show();
base.OnStartup(e);
}

/// <summary>
/// Registers all data types in the container.
/// </summary>
protected virtual void RegisterTypes()
{
// Register INavigationManager service as a singleton.
// Note: It's important to register as a singelton otherwise you will get different navigation managers when you try to navigate to views.
Container.Register<INavigationManager, NavigationManager>(Reuse.Singleton);

// Register views
Container.Register<MainWindow>(Reuse.Singleton);
Container.Register<PageA>(Reuse.Singleton);
Container.Register<PageB>(Reuse.Singleton);

// Register view models
Container.Register<MainWindowViewModel>(Reuse.Singleton);
Container.Register<PageAViewModel>(Reuse.Singleton);
Container.Register<PageBViewModel>(Reuse.Singleton);
}

/// <summary>
/// Registers all navigation views.
/// </summary>
protected virtual void RegisterNavigation()
{
// Resolve singleton instance of the navigation manager from container.
var navigationManager = Container.Resolve<INavigationManager>();

// Register all navigable views (pages, user controls etc) with unique names.
// It will register navigation paths in internal registrar of the navigation manager.
navigationManager.RegisterView<PageA>();
navigationManager.RegisterView<PageB>();
}

#endregion
}
}
14 changes: 14 additions & 0 deletions samples/03-AppNavigation/AppNavigation/AppNavigation.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DryIoc.dll" Version="4.8.0" />
<PackageReference Include="MagicMvvm.Wpf" Version="1.1.0" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions samples/03-AppNavigation/AppNavigation/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Windows.Input;
using MagicMvvm;
using MagicMvvm.Commands;
using MagicMvvm.Navigation;

namespace AppNavigation.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
#region Fields

private readonly INavigationManager _navigationManager;

#endregion

#region Ctor

public MainWindowViewModel(INavigationManager navigationManager)
{
// Resolves from container
_navigationManager = navigationManager;

// Assign action to command using DelegateCommand with command parameter type of string.
NavigateCommand = new DelegateCommand<string>(NavigateToPage);
}

#endregion

#region Commands

public ICommand NavigateCommand { get; }

#endregion

#region Methods

private void NavigateToPage(string page)
{
// Navigate to page view in the region which is "MainRegion"
if (page == "A")
{
_navigationManager.RequestNavigate("MainRegion", "PageA");
}
else if (page == "B")
{
_navigationManager.RequestNavigate("MainRegion", "PageB");
}
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using MagicMvvm;

namespace AppNavigation.ViewModels.Pages
{
public class PageAViewModel : ViewModelBase
{
#region Ctor

public PageAViewModel()
{
PageContent = "Page A content";
}

#endregion


#region Bindable properties

private string _pageContent;
public string PageContent
{
get => _pageContent;
set => SetProperty(ref _pageContent, value);
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using MagicMvvm;

namespace AppNavigation.ViewModels.Pages
{
public class PageBViewModel : ViewModelBase
{
#region Ctor

public PageBViewModel()
{
PageContent = "Page B content";
}

#endregion


#region Bindable properties

private string _pageContent;
public string PageContent
{
get => _pageContent;
set => SetProperty(ref _pageContent, value);
}

#endregion
}
}
40 changes: 40 additions & 0 deletions samples/03-AppNavigation/AppNavigation/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Window x:Class="AppNavigation.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AppNavigation"
mc:Ignorable="d"
Title="App with navigation"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
Height="450"
Width="800">
<Grid>
<StackPanel>
<Frame x:Name="mainFrame"
NavigationUIVisibility="Hidden"
Height="350">
</Frame>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Left"
Margin="10,10,0,0">

<Button Content="Navigate to Page A"
Width="150"
Height="30"
Command="{Binding NavigateCommand}"
CommandParameter="A">
</Button>
<Button Content="Navigate to Page B"
Margin="10,0,0,0"
Width="150"
Height="30"
Command="{Binding NavigateCommand}"
CommandParameter="B">
</Button>
</StackPanel>

</StackPanel>
</Grid>
</Window>
31 changes: 31 additions & 0 deletions samples/03-AppNavigation/AppNavigation/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Windows;
using DryIoc;
using MagicMvvm.Navigation;
using AppNavigation.ViewModels;

namespace AppNavigation.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
// Bind view model to DataContext
DataContext = App.Container.Resolve<MainWindowViewModel>();

// Initialize visual components
InitializeComponent();

// Resolve navigation manager from container
var navigationManager = App.Container.Resolve<INavigationManager>();

// Register navigtion region with unique name and its view instance
navigationManager.RegisterRegionWithView("MainRegion", this.mainFrame);

// Navigate to view "PageA" inside region "MainRegion"
navigationManager.RequestNavigate("MainRegion", "PageA");
}
}
}
19 changes: 19 additions & 0 deletions samples/03-AppNavigation/AppNavigation/Views/Pages/PageA.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Page x:Class="AppNavigation.Views.Pages.PageA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:AppNavigation.Views.Pages"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800"
Title="PageA">

<Grid>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
TextWrapping="Wrap"
FontSize="20"
Text="{Binding PageContent}" />
</Grid>
</Page>
19 changes: 19 additions & 0 deletions samples/03-AppNavigation/AppNavigation/Views/Pages/PageA.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Windows.Controls;
using DryIoc;
using AppNavigation.ViewModels.Pages;

namespace AppNavigation.Views.Pages
{
/// <summary>
/// Interaction logic for PageA.xaml
/// </summary>
public partial class PageA : Page
{
public PageA()
{
// Bind view model to DataContext
DataContext = App.Container.Resolve<PageAViewModel>();
InitializeComponent();
}
}
}
Loading

0 comments on commit 05ed703

Please sign in to comment.