我正在努力弄清楚如何浏览页面。目前,当我按下一个按钮时,它打开一个新的空白页面,而不是我想要的页面。或者我得到这个错误: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();
}
}
}
1条答案
按热度按时间w8f9ii691#
一个页面必须存在于一个主元素中,比如一个“框架”,以便使用窗口的页面导航。
您需要在窗口内有一个框架,并导航到页面。结构为Window -Frame -Pages
在XAML中定义窗口中的Frame元素。定义x:Name="”属性。
在代码隐藏中,将frame.content设置为要显示的页面的示例。
}