How do you navigate to a page using OnFragmentedNavigation in a WPF application?
I want to take in a parameter from the user and navigate to a page created based off that parameter. Here's a simple example:
Let's say you want to navigate to an Employee Profile page when a user gives a certain input.
For simplicity, here's a simple Page view that only has a single textbox with an Employee ID of "1":
<UserControl x:Class="TestSolution.TestPage"
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:mui="http://firstfloorsoftware.com/ModernUI"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Style="{StaticResource ContentRoot}">
<ScrollViewer>
<StackPanel MinWidth="200">
<TextBox Name="Employee ID" Text="1"/> //We want to pass "1"
<Button Click="Button_Click"> //Button that initiates the navigation
</Button>
</StackPanel>
</ScrollViewer>
</Grid>
The code-behind then implements the Button_Click method referred to in the xaml, as well as the IContent methods for Modern UI navigation.
public partial class TestPage : UserControl, IContent
{
public TestPage()
{
InitializeComponent();
}
public void OnFragmentNavigation(FragmentNavigationEventArgs e)
{
int param = Int32.Parse(e.Fragment);
EmployeePage page = new EmployeePage(param);
//when method is triggered, should execute creation of new Employee Page based off parameter
}
public void OnNavigatedFrom(NavigationEventArgs e)
{
//throw new NotImplementedException();
}
public void OnNavigatedTo(NavigationEventArgs e)
{
//throw new NotImplementedException();
}
public void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
//throw new NotImplementedException();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int idParam = Int32.Parse(Test.text);
NavigationCommands.GoToPage.Execute("/EmployeePage.xaml#" + idParam, this);
//parses the text and navigates to an Employee Page, fragmenting the ID
}
}
}
With the Employee page defined as such:
public partial class EmployeePage : UserControl
{
public EmployeePage() { }
public EmployeePage(int id)
{
InitializeComponent();
}
}
My question is- how do you correctly trigger the OnFragmentNavigation method and navigate to the new page while carrying through the ID?
When I run this example, the last line of Button_Click implicitly calls both the "OnNavigatingFrom" and "OnNavigatedFrom" methods, but never "OnFragmentNavigation" even though the navigation link contains the # fragment character specified here: https://github.com/firstfloorsoftware/mui/wiki/Handle-navigation-events
If you can't accomplish this task using OnFragmentNavigation, how else would you do this navigation under the MUI framework?