XAML 试图创建登录页面,但我不能弄清楚如何导航页面

slwdgvem  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(110)

我正在努力弄清楚如何浏览页面。目前,当我按下一个按钮时,它打开一个新的空白页面,而不是我想要的页面。或者我得到这个错误:System.InvalidOperationException:'Window必须是树的根。无法将Window添加为Visual的子级。'
我正在努力让它在用户点击按钮时就能跳转到一个新的页面。我真的很感激任何能帮到我的人。
XAML(顺便说一句,这不是全部代码,我只是取了我认为重要的部分)

<Window x:Class="Movie_Appp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Movie_Appp"
        mc:Ignorable="d"
        Title="Main"
        Height="450" 
        Width="800"
        Name="Main"
        WindowStyle="None"
        ResizeMode="NoResize"
        WindowStartupLocation="CenterScreen"
        Background="Transparent"
        AllowsTransparency="True"
        MouseDown="Window_MouseDown">
<Button x:Name="BtnLogin" 
      BorderThickness="0"
      Content="Login"
      Foreground="White"
      FontSize="12"
      FontFamily="Montserrat"
      Cursor="Hand"
      Click="BtnLogin_Click"
      Margin="0, 15,0,0">

  <Button.Style>
      <Style TargetType="{x:Type Button}">
          <Setter Property="Background" Value="#462AD8"/>
          <Style.Triggers>
              <Trigger Property="IsMouseOver" Value="True">
                  <Setter Property="Background" Value="#28AEED"/>
              </Trigger>
          </Style.Triggers>
      </Style>
  </Button.Style>

  <Button.Template>
      <ControlTemplate TargetType="{x:Type Button}">
          <Border Width="150" Height="40"
                  CornerRadius="20"
                  Background="{TemplateBinding Background}">
              <ContentPresenter VerticalAlignment="Center"
                                HorizontalAlignment="Center"/>
          </Border>
      </ControlTemplate>
  </Button.Template>
</Button>

C#

using Movie_Appp.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Movie_Appp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnLogin_Click(object sender, RoutedEventArgs e)
        {
            // Create a new instance of LoginView
            LoginView loginView = new LoginView();

            // Show the new window
            loginView.Show();
        }

        private void BtnSignUp_Click(object sender, RoutedEventArgs e)
        {
            // Create a new instance of SignUpView
            SignUpView signUpView = new SignUpView();

            // Show the new window
            signUpView.Show();
        }



        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        private void btnMinimize_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}
w8f9ii69

w8f9ii691#

一个页面必须存在于一个主元素中,比如一个“框架”,以便使用窗口的页面导航。
您需要在窗口内有一个框架,并导航到页面。结构为Window -Frame -Pages
在XAML中定义窗口中的Frame元素。定义x:Name="”属性。
在代码隐藏中,将frame.content设置为要显示的页面的示例。

public partial class MainWindow : Window
{
//initialize page variables to hold instances
    private Page1 firstpage;
    private Page2 page2;
    public MainWindow()
    {

        InitializeComponent();
//create instance of a page
        page1 = new Page1();
//set frame element defined in XAML content to initialized page
        frame_element_name.Content = Page1;
        firstpage = frame_element_name.Content as Page1;
        
    }

    private void button_click(object sender, e eventargs)
    {
//initialize new page 
        Page2 newpage = new Page2(); //newpage_X if subseuent instance of same page
 // or page2 = oldpage; // or pagewhateverpage_X etc...

frame_element_name.Content = page2; // or = pagewhateverpage_X
    }

    private void other_methods(...)
    {
    //to access it elsewhere after initialised and navigated away from, refer to to the frames previous content rather than a new instance of page to retrieve in the state or just ensure the frame Navigation is set to visible and the frames Navigation Journal is caching the navigation Journal Entries and/or Forward/Back stack items
        if(page_name!=null)
        {
            frame_elemnet_name.Content = page_name;
//or frame_element_name.Navigate(initialized and previously displayed page);
        }
    else if (page_name == null)
    {
        frame.Content = new Pagenametype();
        pagename = frame_element_name.Content as Pagenametype;
     }
 }

}

相关问题