C# 如何使用字符串作为此.Frame.navigate(typeof()的参数在XAML中导航?
在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>
<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()));
}