【发布时间】:2017-05-02 03:40:39
【问题描述】:
如何在 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();
}
}
我的问题是——如何正确触发 OnFragmentNavigation 方法并在携带 ID 时导航到新页面?
当我运行此示例时,Button_Click 的最后一行隐式调用“OnNavigatingFrom”和“OnNavigatedFrom”方法,但从不调用“OnFragmentNavigation”,即使导航链接包含指定的 # 片段字符这里:https://github.com/firstfloorsoftware/mui/wiki/Handle-navigation-events
如果你不能使用 OnFragmentNavigation 完成这个任务,你会如何在 MUI 框架下完成这个导航?
【问题讨论】:
-
对不起。您的编辑足以让我知道您正在做的事情与您最初发布问题时的样子完全不同,而且我仍然没有足够的上下文来真正理解这个问题。也许其他人会捡起它。
-
上下文不够怎么办?你还需要什么?
-
一个实际的minimal reproducible example 将是一个好的开始。但是您似乎正在使用某种第三方库,因此您的问题可能涉及他们的库特有的一些问题。我对库不熟悉,所以即使你改进了问题,也不能保证我能够提供解决方案(即有时间熟悉第三方库以理解和回答问题) .你最好找一个已经知道图书馆的人。
-
这绝对是一个 MCVE。我提供了一个非常分解的示例,它仍然说明了我对 cmets 的整个问题以及每个文件的描述。第二点 - 第三方的名称(“现代 UI”或“MUI”)始终在问题标题、问题标签和整个问题中说明。
-
请阅读文章minimal reproducible example。另请阅读How to Ask,尤其是该页面底部链接的文章。您对构成实际 MCVE 的理解存在缺陷。