在现代UI WPF应用程序中导航到带有参数的页面

时间:2017-05-02 03:40:40

标签: c# wpf modern-ui

如何在WPF应用程序中使用OnFragmentedNavigation导航到页面? 我想从用户接收参数并导航到基于该参数创建的页面。这是一个简单的例子:


假设您想在用户提供特定输入时导航到“员工个人资料”页面。

为简单起见,这是一个简单的页面视图,只有一个文本框,其员工ID为" 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>

然后,代码隐藏实现了xaml中引用的Button_Click方法,以及现代UI导航的IContent方法。

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

}

将Employee页面定义为:

    public partial class EmployeePage : UserControl
    {
     public EmployeePage() { }

     public EmployeePage(int id)
     {
        InitializeComponent();
     }
    }

我的问题是 - 如何正确触发OnFragmentNavigation方法并在传输ID时导航到新页面?

当我运行这个例子时,Button_Click的最后一行隐式调用&#34; OnNavigatingFrom&#34;和&#34; OnNavigatedFrom&#34;方法,但从来没有&#34; OnFragmentNavigation&#34;即使导航链接包含此处指定的片段字符:https://github.com/firstfloorsoftware/mui/wiki/Handle-navigation-events

如果您无法使用OnFragmentNavigation完成此任务,您还将如何在MUI框架下执行此导航?

3 个答案:

答案 0 :(得分:0)

我有一个示例on github。启动它,检查与Grid控件相关的事件。

答案 1 :(得分:0)

EmplyeePage必须实现IContent接口。在这里您会发现OnFragmentNavigation。

在您浏览的页面中会调用

OnNavigatedFromOnNavigatingFrom

在您导航到的页面中会调用

OnNavigatedToOnFragmentNavigation

永远不会调用参数化构造函数,但我想这么做。

答案 2 :(得分:0)

类似于PRISM View-Base Navigation
在导航命令(或服务)中,您需要创建 NavigationParameters ,然后可以通过 _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);  
}

必须实现INavigationAware的内部视图,您可以检索 NavigationEventArgs

中的 NavigationParameters
public void OnNavigatedTo(NavigationContext navigationContext)
{
    string id = navigationContext.Parameters["ID"];
    ObjectParameter myParameter =navigationContext.Parameters["myObjectParameter"];
}