Microsoft Docs: How to: Navigate to a Page
Microsoft Docs: NavigationWindow Class
Хабр: WPF Navigation: Используем связку Page + Frame
namespace PageNavigation.Services
{
public interface INavigationService
{
void NavigateToPage1();
void NavigateToPage2();
}
}
using System.Windows.Controls;
using PageNavigation.Pages;
namespace PageNavigation.Services
{
public class NavigationService : INavigationService
{
private readonly Frame _frame;
public NavigationService(Frame frame)
{
_frame = frame;
}
public void NavigateToPage1()
{
_frame.Navigate(new Page1());
}
public void NavigateToPage2()
{
_frame.Navigate(new Page2());
}
}
}
Такой Ioc:
using System.Windows.Controls;
namespace PageNavigation.Services
{
public static class Ioc
{
public static INavigationService NavigationService { get; private set; }
public static void Init(Frame frame)
{
NavigationService = new NavigationService(frame);
}
}
}
Или такой Ioc:
https://autofac.org/
using System.Windows.Controls;
using Autofac;
namespace PageNavigation.Services
{
public static class Ioc
{
private static IContainer _container;
public static INavigationService NavigationService
{
get { return _container.Resolve<INavigationService>(); }
}
//public static MainViewModel MainViewModel
//{
// get { return _container.Resolve<MainViewModel>(); }
//}
public static void Init(Frame frame)
{
var builder = new ContainerBuilder();
builder.RegisterType<NavigationService>()
.As<INavigationService>()
.SingleInstance()
.WithParameter(new TypedParameter(typeof(Frame), frame));
//builder.RegisterType<MainViewModel>()
// .SingleInstance();
_container = builder.Build();
}
}
}
Главное окно XAML
<Window
x:Class="PageNavigation.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"
Title="MainWindow"
Width="525"
Height="350"
Loaded="OnLoaded"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="266*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Frame x:Name="_frame" />
<StackPanel
Grid.Row="1"
Margin="8,0,0,6"
Orientation="Horizontal">
<Button
MinWidth="75"
MinHeight="29"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="OnNavigateToPage1ButtonClick"
Content="Page1" />
<Button
MinWidth="75"
MinHeight="29"
Margin="8,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="OnNavigateToPage2ButtonClick"
Content="Page2" />
</StackPanel>
</Grid>
</Window>
Главное окно C#
using System.Windows;
using PageNavigation.Services;
namespace PageNavigation
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
Ioc.Init(_frame);
}
private void OnNavigateToPage1ButtonClick(object sender, RoutedEventArgs e)
{
Ioc.NavigationService.NavigateToPage1();
}
private void OnNavigateToPage2ButtonClick(object sender, RoutedEventArgs e)
{
Ioc.NavigationService.NavigateToPage2();
}
}
}
Страница 1 XAML
<Page
x:Class="PageNavigation.Pages.Page1"
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"
Title="Page1"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid>
<TextBlock Margin="5">
Это Page 1
</TextBlock>
<Button
Padding="12,3,12,3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Click="OnNavigateToAnotherPageButtonClick"
Content="Перейти на Page 2" />
</Grid>
</Page>
Страница 1 C#
using System.Windows.Controls;
using PageNavigation.Services;
namespace PageNavigation.Pages
{
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void OnNavigateToAnotherPageButtonClick(object sender, System.Windows.RoutedEventArgs e)
{
Ioc.NavigationService.NavigateToPage2();
}
}
}
Страница 2 XAML
<Page
x:Class="PageNavigation.Pages.Page2"
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"
Title="Page2"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid>
<TextBlock Margin="5">
Это Page 2
</TextBlock>
<Button
Padding="12,3,12,3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Click="OnNavigateToAnotherPageButtonClick"
Content="Перейти на Page 1" />
</Grid>
</Page>
Страница 2 C#
using System.Windows;
using System.Windows.Controls;
using PageNavigation.Services;
namespace PageNavigation.Pages
{
public partial class Page2 : Page
{
public Page2()
{
InitializeComponent();
}
private void OnNavigateToAnotherPageButtonClick(object sender, RoutedEventArgs e)
{
Ioc.NavigationService.NavigateToPage1();
}
}
}