如何使用字符串作为此参数在 XAML 中进行导航.Frame.Navigate(typeof().

本文关键字:导航 Frame Navigate typeof XAML 字符串 何使用 参数 | 更新日期: 2023-09-27 18:36:32

在我有的 Xaml 文件中

<ComboBox Grid.Row="2" Grid.Column="2" Margin="50,50,50,50" FontSize="50" SelectionChanged="NavigateKing">
        <ComboBoxItem></ComboBoxItem>
        <ComboBoxItem Content="Prince Wijaya"></ComboBoxItem>
        <ComboBoxItem Content="Pandukabhaya"></ComboBoxItem>
        <ComboBoxItem Content="Dewanampiya Tissa"></ComboBoxItem>
    </ComboBox>

并在 Xaml.Cs 文件中

private void NavigateKing(object sender, SelectionChangedEventArgs e)
    {
        ComboBoxItem cbi = (ComboBoxItem)((sender as ComboBox).SelectedItem);
        string navigatingURI = cbi.Content.ToString();
        this.Frame.Navigate(typeof(/*IntendedXaml filename should be here*/);
    } 

与其编写一个完整的 if-else 或切换案例场景,我是否可以只传递 NavigationURI 进行导航?

如何使用字符串作为此参数在 XAML 中进行导航.Frame.Navigate(typeof().

试试这个。

XAML

<ComboBox Margin="50,50,50,50" FontSize="50" SelectionChanged="NavigateKing">
    <ComboBoxItem Content="Prince Wijaya" Tag="BlankPage1"></ComboBoxItem>
    <ComboBoxItem Content="Pandukabhaya" Tag="BlankPage2"></ComboBoxItem>
    <ComboBoxItem Content="Dewanampiya Tissa" Tag="BlankPage3"></ComboBoxItem>
</ComboBox>

C#

private void NavigateKing(object sender, SelectionChangedEventArgs e)
{
    ComboBoxItem cbi = (ComboBoxItem)((sender as ComboBox).SelectedItem);
    string navigatingURI = cbi.Content.ToString();
    this.Frame.Navigate(Type.GetType(this.GetType().Namespace + "." + cbi.Tag.ToString()));
}