嘿,你好! 我是 wpf 和 c# 的新手。 我现在已经花了 2 天的时间在网上搜索,没有任何解决方案可以以令人满意的方式实施。要么我无法使解决方案起作用,要么是烦人的 MVVM 模式。在我目前的学习步骤中,它太抽象了。 因此,我创建了一个新应用程序,其中包含制作所需功能所需的最简单元素。 它是一个 WPF 应用/.Net 4.8 框架。 带有 Frame 和 UserControl 的 MainWindow。 3 页: 带有 2 个按钮的 LoginPage,可以在 MainWindow 的框架中打开其他 2 个页面: LoginSubPage1 LoginSubPage2 和 UserControl “BottomBar” 包含 3 个按钮: 返回 首页 出口
MainWindow.xaml:
<Window x:Class="UserControl2020_12_15.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uc="clr-namespace:UserControl2020_12_15"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="600" WindowStartupLocation="CenterScreen"
Background="CadetBlue">
<Grid>
<Frame x:Name="PageFrame" NavigationUIVisibility="Hidden"
VerticalAlignment="Top" Height="250"/>
<uc:BottomBar VerticalAlignment="Bottom" Height="80"/>
</Grid>
</Window>
Loginpage.xaml:
<Page x:Class="UserControl2020_12_15.LoginPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel>
<Button Content="LoginSubPage1" Click="OneClick" Background="Red"/>
<Button Content="LoginSubPage2" Click="TwoClick" Background="Green"/>
</StackPanel>
</Page>
Loginsubpage1.Xaml:
<Page x:Class="UserControl2020_12_15.LoginSubPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Background="Red">
</Page>
Loginsubpage2.Xaml:
<Page x:Class="UserControl2020_12_15.LoginSubPage2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Background="Green">
</Page>
Bottomber.Samel:
<UserControl x:Class="UserControl2020_12_15.BottomBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="80" d:DesignWidth="800">
<Grid>
<Button Content="BackBtn" HorizontalAlignment="Left" Margin="20,0,0,0"
Click="BackClick" Height="50"/>
<Button Content="HomeBtn" HorizontalAlignment="Center" Margin="0"
Click="HomeClick" Height="50"/>
<Button Content="ExitBtn" HorizontalAlignment="Right" Margin="0,0,20,0"
Click="ExitClick" Height="50"/>
</Grid>
</UserControl>
MainWindow.xaml.cs:
using System.Windows;
namespace UserControl2020_12_15
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoginPage loginPage = new LoginPage();
PageFrame.NavigationService.Navigate(loginPage);
}
}
}
Lokinbage.Semal.CS:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
namespace UserControl2020_12_15
{
public partial class LoginPage : Page
{
public LoginPage()
{
InitializeComponent();
}
private void OneClick(object sender, RoutedEventArgs e)
{
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new Uri("LoginSubPage1.xaml", UriKind.RelativeOrAbsolute));
}
private void TwoClick(object sender, RoutedEventArgs e)
{
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new Uri("LoginSubPage2.xaml", UriKind.RelativeOrAbsolute));
}
}
}
Bottomer.Semal.CS:
using System.Windows;
using System.Windows.Controls;
namespace UserControl2020_12_15
{
public partial class BottomBar : UserControl
{
public BottomBar()
{
InitializeComponent();
}
private void BackClick(object sender, RoutedEventArgs e)
{
}
private void HomeClick(object sender, RoutedEventArgs e)
{
}
private void ExitClick(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
}
}
HomeClick 应该在 Frame 中打开 LoginPage.xaml,或者重新加载应用程序,以便它回到框架包含 LoginPage.xaml 的位置,无论我们在应用程序中的哪个位置 ,BackClick 都应该返回到上次查看的页面
我不想在 MVVM 构建中制作它,而只是在 BottomBar 后面的代码中制作它。
Note:此问题总结整理于:How to make a Home-button in UserControl switch a page in MainWindow frame without using mvvm?