N
N
Nikita2020-10-12 09:23:25
WPF
Nikita, 2020-10-12 09:23:25

How to navigate between pages in Windows Presentation Foundation?

In trying to find an answer to my question, the only thing that I have achieved is the ability to navigate to any page in the way below: However, I do not know how to implement the ability to return to the main page (display the MainWindow content).
Main.Content = new Page();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris the Animal, 2020-10-12
@MiiZZo

Microsoft Docs: How to: Navigate to a Page Microsoft Docs
: NavigationWindow Class

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());
        }
    }
}

Such 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);
        }
    }
}

Or this 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 main window
<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>

Main 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();
        }
    }
}

Page 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>

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();
        }
    }
}

Page 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>

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();
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question