【问题标题】:How can i navigate one xaml page to another?如何将一个 xaml 页面导航到另一个页面?
【发布时间】:2010-04-25 18:04:13
【问题描述】:

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

我在 App.xaml 中添加了此代码:

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

然后我在 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?

【问题讨论】:

    标签: silverlight xaml silverlight-4.0 c#-4.0


    【解决方案1】:

    假设您正在查看MainPage.xaml,然后您想通过单击ButtonImageEdit 中的MainPage.xaml 打开另一个名为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;
    }
    

    这对我有用。

    【讨论】:

    • 也为我工作...如果您已经拥有用于按钮控制的 private void Button_Click_1(object sender, RoutedEventArgs e),则不要复制第一行,否则它会完美且简单。
    • 虽然它一直在工作,但 URL 似乎没有改变。
    【解决方案2】:

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

    首先,您需要在项目中添加对 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

    &lt;Button Click="Button_Click" Tag="/Views/Second.xaml" Content="Second" /&gt;

    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,浏览器的后退和前进按钮可以完美运行,并且地址栏中的 URL 会根据您当前所在的 XAML 页面进行更新:)

    【讨论】:

      【解决方案3】:

      看来您的出发点是错误的。此类事情适用于使用导航应用程序模板。您应该开始一个新项目并选择“Silverlight Navigation Application”。

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

      【讨论】:

        【解决方案4】:

        解决这个问题的简单方法,你可以看看这个网站:http://blogs.microsoft.co.il/blogs/eladkatz/archive/2011/01/25/adapting-silverlight-navigation-to-mvvm.aspx

        我之前遇到过这个问题。但是在阅读完本教程后,我可以使用 MVVM 轻松导航到另一个视图。希望这可以帮助大家解决问题。Thx

        【讨论】:

          【解决方案5】:
          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)); 
                  }
          
              }
          }
          

          【讨论】:

            【解决方案6】:

            试试这个:

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

            它对我有用。 :)

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-02-16
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多