-1

I'm new to C# and object orientated coding therefore I was wondering how to create a routing class for navigation between forms in a .Net Framework WPF application.

At the moment I am navigating between forms like this:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        new Classes.SessionService();
    }
    private void Customer_Page_click(object sender, RoutedEventArgs e)
    {
        CustomerPage CP = new CustomerPage();
        CP.Show();
        this.Close();
    }
    private void Staff_Menu_click(object sender, RoutedEventArgs e)
    {
        Forms.StaffPage SP = new Forms.StaffPage();
        SP.Show();
        this.Close();
    }

    private void Exit_click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}

I have been told it is better to create a routing class as it will separate code more, but I have not been able to find an example for navigating between forms but only between web pages.

Pawel
  • 141
  • 1
  • 10

1 Answers1

-1

Im strongly recommend you to use an existing library bevor starting to create your own infrastructure. (I've learnd it the hard way and ended up using prism ;) ) There are other great libraries out there, too. Just go to youtube and search for "c# prism outlook" there is a pretty good prism tutorial from brian lagunas.

Edit: Just to give you an basic idea how you can solve this by yourself:

Main Window xaml:

<Window x:Class="WpfTestApp.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:WpfTestApp"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <WrapPanel Grid.Row="0" x:Name="Ribbon"/>
    <ContentPresenter Grid.Row="1" x:Name="Content"/>
    <StackPanel Grid.Row="2">
        <Button Grid.Row="0" Content="SetContentFormA"  Click="Button_Click"/>
        <Button Grid.Row="0" Content="SetContentFormB"  Click="Button_Click_1"/>
        <Button Grid.Row="0" Content="AddNewFormAToRibbon"  Click="Button_Click_2"/>
        <Button Grid.Row="0" Content="RemoveFormBFromRibbon"  Click="Button_Click_3"/>
    </StackPanel>
</Grid>

MainWindow.cs:

 public partial class MainWindow : Window
{
    private FormB b;

    public MainWindow()
    {
        InitializeComponent();

        PageControler.Content = Content;
        PageControler.Ribbon = Ribbon;

        b = new FormB();

        Ribbon.Children.Add(b);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        PageControler.SetContent(new FormA());
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        PageControler.SetContent(new FormB());
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        PageControler.AddToRibbon(new FormA());
    }

    private void Button_Click_3(object sender, RoutedEventArgs e)
    {
        PageControler.RemoveFromRibbon(b);
    }
}

PageControler:

public static class PageControler
{
    public static ContentPresenter Content { get; set; }
    public static WrapPanel Ribbon { get; set; }


    public static void SetContent(UserControl control)
    {
    Content.Content = control;
    }

    public static void AddToRibbon(UserControl control)
    {
        Ribbon.Children.Add(control);
    }

    public static void RemoveFromRibbon(UserControl control)
    {
        Ribbon.Children.Remove(control);
    }
}

FormA (& B):

<UserControl x:Class="WpfTestApp.FormA"
         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:WpfTestApp"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="Violet">
    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="Form A"/>
</Grid>

You can althou

  • I will save this and review his videos but for the moment I am trying to use 'clean' C# so without using any existing libraries etc. mainly because its for a course I am doing and at the moment we are covering basics, if that makes sense? – Pawel Apr 07 '20 at 10:04
  • Well i understand this! :) I have updated the answer with some basic example, how you could solve navigation with UserControls or do you want to create navigation between pages? – NX ENGINEERING Apr 07 '20 at 11:43
  • That works, I understand it a bit more now after having a look at your PageController. I guess that answers my question. I have also found this question which is quite helpful: https://stackoverflow.com/questions/12206120/window-vs-page-vs-usercontrol-for-wpf-navigation – Pawel Apr 07 '20 at 12:15