I am very new to C# and the WPF Architecture. I am trying to create an application with simple page Navigation. I have changed my MainWindow from Window to NavigationWindow. I have also put the following code in MainWindow.xaml.cs
public void View(string newView)
{
Uri view = new Uri(newView, UriKind.Relative);
NavigationService nav = NavigationService.GetNavigationService(this);
if (nav != null)
{
nav.Navigate(view);
}
else
{
MessageBox.Show("NavNull");
}
}
I am calling this method from the default source ("HubView.xaml") this is in a folder called "Pages". The code looks like this
public void Button_Click(object sender, RoutedEventArgs e)
{
View = @"\Pages\UserAdd.xaml";
MainWindow mainWindow = new MainWindow();
mainWindow.View(View);
}
However when I click the button the message box is displayed indicating that nav equals null. I have read through this question and the subsequent answer, however I still do not fully understand.
Any suggestions would be greatly appreciated!
Edit:
This is the "MainWindow.xaml":
<NavigationWindow x:Class="Container.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ResizeMode="CanMinimize"
Title="MainWindow" Height="350" Width="525" Source="Pages/HubView.xaml" WindowStyle="ThreeDBorderWindow" ShowsNavigationUI="False">
</NavigationWindow>
As you can see the Window is a Navigation Window.
NavigationService
is for page navigation in WPF like in a browser and for that to work you would need to add a Frame
to your MainWindow.xaml
<Grid>
<Frame x:Name="mainFrame"/>
<Button x:Name="btnPage1" Content="Page 1" Width="200" Height="50"
Click="btnPage1_Click"/>
</Grid>
and use NavigationService
to navigate eg
private void btnPage1_Click(object sender, RoutedEventArgs e)
{
mainFrame.NavigationService.Navigate(new Uri("Page1.xaml", UriKind.Relative));
}
EDIT Create 1 window and 2 pages
MainWindow.xaml
<NavigationWindow x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Source="Page1.xaml">
Page1.xaml
<Page x:Class="WpfApplication1.Page1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<TextBlock Text="Page 1" FontWeight="Bold" />
<Button Margin="0,40" Content="Goto Page 2" Width="200" Height="50"
Click="Button_Click"/>
</Grid>
Page1.xaml.cs
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative));
}
}
NavigationWindow
has a property of NavigationService
so no need to create new variables. Hope that helps. NOTE Page2.xaml is just a blank page
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.