2010-04-25 260 views
9

我有2页我需要浏览mainpage.xaml到login.page xaml,但它会抛出我 对象引用未设置为对象的实例。在Root.Children.Clear(); ....如何导航一个xaml页面到另一个页面?

我加入这个代码在App.xaml中:

private void Application_Startup(object sender, StartupEventArgs e) 
     { 
      Grid myGrid = new Grid(); 
      myGrid.Children.Add(new MainPage()); 
      this.RootVisual = myGrid; 
     }

和比我ADDE上main.xaml一些码来导航到LoginUI.xaml

namespace Gen.CallCenter.UI 
{ 
    public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 

      Grid Root = ((Grid)(this.Parent)); 
      Root.Children.Clear(); 
      Root.Children.Add(new LoginUI()); 
     } 
    } 
}

如何将main.xaml导航到LoginUI.xaml?

回答

11

AnthonyWJones说你需要使用导航框架。

首先,您需要参考您的项目和refernce它添加到System.Windows.Controls.Navigation你MainPage.xaml中

xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 

然后你需要中,你会切换不同的XAML页面的框架。事情是这样的:

<navigation:Frame x:Name="navFrame" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Source=”/Views/First.xaml” /> 

现在某处MainPage.xaml中,你可以有一个按钮与标签

<Button Click="Button_Click" Tag="/Views/Second.xaml" Content="Second" />

,并在Button_Click事件处理程序,你可以切换出内容显示,navFrame

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    Button theButton = sender as Button; 
    string url = theButton.Tag.ToString(); 

    this.navFrame.Navigate(new Uri(url, UriKind.Relative)); 
} 

清凉一点要注意的是,通过使用NavigationFramework浏览器的前进和后退按钮完美的工作,并根据不同的XAML页面上的地址栏更新网址您目前的:)

2

看起来你开始走错了路。这种事情是照顾使用导航应用程序模板。您应该开始一个新项目并选择“Silverlight导航应用程序”。

一旦加载,只需运行它即可查看基本shell的外观。然后看看MainPage是如何构建的,并说出Home视图。您需要做的是基于导航Page类型创建新视图,然后将它们添加到MainPage.xaml。

1
private void formcombobox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    foreach (ComboBoxItem child in formcombobox.Items) 
    { 
     if (child.Name != null && child.IsSelected == true) 
     { 

      string url = new System.Uri("/DWRWefForm;component/Pages/" 
          + child.Name + ".xaml", System.UriKind.Relative).ToString(); 
      this.navframe.Navigate(new Uri(url, UriKind.Relative)); 
     } 

    } 
} 
12

假设您正在查看的MainPage.xaml那么你想通过点击ButtonMainPage.xamlImageEdit开叫newPage.xaml另一个XAML页面,这里是快速的解决方案,你应写在MainPage.xaml.cs内:

private void imageEdit1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    newPage mynewPage = new newPage(); //newPage is the name of the newPage.xaml file 
    this.Content = mynewPage; 
} 

这是与我合作。

+0

也适用于我......如果您已经有'private void Button_Click_1(object sender,RoutedEventArgs e)'进行按钮控制,那么请不要复制第一行,除此之外,它是完美和容易的。 – DevCompany 2012-10-17 21:47:33

+0

虽然它一直在工作,但URL似乎没有改变。 – 2015-03-11 14:57:15

1

试试这个:

private void imageEdit1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    newPage mynewPage = new newPage(); //newPage is the name of the newPage.xaml file 
    this.Content = mynewPage; 
} 

它为我工作。 :)

相关问题