1

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?

Greg C.
  • 19
  • 5
  • Sorry. Your edit is enough for me to know that you're doing something completely different from what it looked like in your initial effort to post your question, but also that there's still not enough context for me to really understand the question. Maybe someone else will pick it up. – Peter Duniho May 04 '17 at 06:11
  • How is there not enough context? What else would you need? – Greg C. May 04 '17 at 17:42
  • An actual [mcve] would be a good start. But you appear to be using some sort of third-party library, and so your question may involve some issue specific to their library. I'm unfamiliar with the library, so even if you improved the question, there's no guarantee I'd be able to provide a solution (i.e. have the time to become familiar enough with the third-party library to understand and answer the question). You're better off finding someone who already knows the library. – Peter Duniho May 04 '17 at 18:23
  • This is definitely an MCVE. I provided a VERY broken down example that still illustrates my entire problem with comments and a description for each file. To your second point- the name of the third party ("Modern UI" or "MUI") has always been stated in the question title, the question tags, and throughout the question. – Greg C. May 04 '17 at 19:04
  • Please read the article [mcve]. Also read [ask], and especially the articles linked at the bottom of that page. Your understanding of what constitutes an actual MCVE is flawed. – Peter Duniho May 04 '17 at 19:18

3 Answers3

0

I have a sample that does this on github. Fire it up, check out the events tied to the Grid control.

Rich Bryant
  • 865
  • 10
  • 27
0

EmplyeePage must implement IContent interface. There's where you'll catch OnFragmentNavigation.

OnNavigatedFrom and OnNavigatingFrom are called in the page you are navigating from.

OnNavigatedTo and OnFragmentNavigation are called in the page you are navigating to.

Parameterized constructor won't ever be called, but I'd like to.

Antonio Rodríguez
  • 976
  • 2
  • 11
  • 25
0

It similar to PRISM View-Base Navigation
In your navigation command (or service) you need to create NavigationParameters and than you can pass it via _regionManager.RequestNavigate (regionName, uri, navigationParameters)

public void NavigationCommandExecute()
{    
        var parameters = new NavigationParameters(); 
            parameters.Add("ID", employee.Id); 
            parameters.Add("myObjectParameter", new ObjectParameter());
            regionManager.RequestNavigate(RegionNames.TabRegion, new Uri("EmployeeDetailsView", UriKind.Relative), parameters);  
}

Inside View which must implement INavigationAware you can retrieve NavigationParameters from NavigationEventArgs

public void OnNavigatedTo(NavigationContext navigationContext)
{
    string id = navigationContext.Parameters["ID"];
    ObjectParameter myParameter =navigationContext.Parameters["myObjectParameter"];
}
Anton
  • 718
  • 6
  • 11