[英]Navigating to page with parameter in modern UI WPF application
如何在 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
}
}
}
員工頁面定義如下:
public partial class EmployeePage : UserControl
{
public EmployeePage() { }
public EmployeePage(int id)
{
InitializeComponent();
}
}
我的問題是 - 如何在攜帶 ID 的同時正確觸發 OnFragmentNavigation 方法並導航到新頁面?
當我運行此示例時,Button_Click 的最后一行隱式調用“OnNavigatingFrom”和“OnNavigatedFrom”方法,但從不調用“OnFragmentNavigation”,即使導航鏈接包含此處指定的#fragment字符: https ://github.com/ firstfloorsoftware/mui/wiki/Handle-navigation-events
如果您無法使用 OnFragmentNavigation 完成此任務,您將如何在 MUI 框架下完成此導航?
我在 github 上有一個執行此操作的示例。 啟動它,檢查與 Grid 控件相關的事件。
EmplyeePage 必須實現 IContent 接口。 您將在此處找到 OnFragmentNavigation。
在您導航的頁面中調用OnNavigatedFrom
和OnNavigatingFrom
。
在您導航到的頁面中調用OnNavigatedTo
和OnFragmentNavigation
。
參數化構造函數永遠不會被調用,但我願意。
它類似於 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"];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.