Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用字符串作为此.Frame.navigate(typeof()的参数在XAML中导航?_C#_Windows_Xaml_Uinavigationcontroller_Windows Store Apps - Fatal编程技术网

C# 如何使用字符串作为此.Frame.navigate(typeof()的参数在XAML中导航?

C# 如何使用字符串作为此.Frame.navigate(typeof()的参数在XAML中导航?,c#,windows,xaml,uinavigationcontroller,windows-store-apps,C#,Windows,Xaml,Uinavigationcontroller,Windows Store Apps,在Xaml文件中,我有 <ComboBox Grid.Row="2" Grid.Column="2" Margin="50,50,50,50" FontSize="50" SelectionChanged="NavigateKing"> <ComboBoxItem></ComboBoxItem> <ComboBoxItem Content="Prince Wijaya"></ComboBoxItem>

在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>
与其编写一个完整的if-else或switch-case场景,不如将NavigationURI传递给navigate?

试试这个

XAML

试试这个

XAML


您可以将实际类型存储在
ComboBoxItem
Tag
属性上,并使用
Tag
SelectedItem
进行导航。@JeffBridgman您能不能请您举个例子呢?当然不能。我对XAML没有任何经验,但我在WinForms中做过类似的事情,并且看到
标记
属性存在,因此假设它是可能的。我们可以使用
this.Navigate(//URI在这里)
来实现这一点,但不幸的是,在Windows 8应用程序中只有
this.Frame.Navigate()
可用..您可以将实际类型存储在
ComboBoxItem
标记上,并使用
标记对
SelectedItem
进行导航。@JeffBridgman您能不能请您举个例子呢?当然不能。我对XAML没有任何经验,但我在WinForms中做过类似的事情,也看到过类似的情况由于
标记
属性存在,因此假设它是可能的。我们可以使用
this.Navigate(//URI在这里)
来实现这一点,但不幸的是,在Windows 8应用程序中,只有
this.Frame.Navigate()
可用。上面的代码给了我一个异常,在此行this.Frame.Navigate中,对象没有像oneNullReferenceException那样被引用(Type.GetType(this.GetType().Namespace+“+cbi.Tag.ToString());}我没有提到所有XAML都驻留在一个名为Pages的文件夹中吗?上面的代码给了我一个异常,在这一行this.Frame.Navigate(Type.GetType(this.GetType().Namespace+“+cbi.Tag.ToString())中没有引用像oneNullReferenceException一样的对象)}我没有提到所有XAML都驻留在一个名为Pages的文件夹中吗?
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*/);
    } 
<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>
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()));
}