有没有办法通过点击Form1按钮在主窗口上显示Form2



我已经设法在MainWindow上的网格中打开Form1,但现在我需要关闭Form1,并在单击Form1上的按钮后在MainWindow的同一网格中显示Form2。

如何做到这一点?

主窗口代码:

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.Shapes;
using System.Windows.Forms;
using System.Configuration;
using Application = System.Windows.Application;
using System.Windows.Forms.Integration;
namespace SolAquaPro
{
/// <summary>
/// Interaction logic for winHomeDash.xaml
/// </summary>
public partial class WinHomeDash : Window
{
public WinHomeDash()
{
InitializeComponent();
}
public void BtnFuel_Click(object sender, RoutedEventArgs e)
{
FuelDash fd1 = new FuelDash();
fd1.TopLevel = false;
WindowsFormsHost host1 = new WindowsFormsHost();
host1.Child = fd1;
stackForms.Children.Add(host1);            
}
}
}

主窗口设计代码为:

<Window x:Class="SolAquaPro.WinHomeDash"
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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:local="clr-namespace:SolAquaPro"
mc:Ignorable="d"
Title="winHomeDash" Height="650" Width="950"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
materialDesign:ThemeAssist.Theme="Dark"
Background="{x:Null}" Loaded="BtnMenuShort_Click">
<materialDesign:Card UniformCornerRadius="15" Margin="25" Width="900" Height="600" Background="{DynamicResource ResourceKey=MaterialDesignDarkBackground}">

<materialDesign:Card UniformCornerRadius="15" HorizontalAlignment="Left" Width="225" Background="#0A1828">

<DockPanel Width="225" Height="600">
<StackPanel Height="155" VerticalAlignment="Top" Width="225">
<Image Height="50" Width="50" Margin="0 15 0 15" Source="/UserPic.png"/>
<Label Name="lblName" BorderThickness="0" FontFamily="Arial" FontWeight="Bold" FontSize="17" Margin="0 0 0 5" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="#FF2372FA" Height="24" Loaded="Window_Loaded"/>
<Label x:Name="lblDateTime" Content="" FontFamily="Arial" FontWeight="SemiBold" FontSize="12" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="#FF2372FA" Margin="0 0 0 18" Height="22" Width="225" Loaded="LblDateTime_Loaded"/>
<Rectangle Name="rec1" Height="7" Fill="#FF2372FA"/>
</StackPanel>
<materialDesign:Card Name="card2" Width="210" UniformCornerRadius="15" Height="445" Background="{DynamicResource ResourceKey=MaterialDesignDarkBackground}" Foreground="White" Margin="-210,155,0,0">
<StackPanel VerticalAlignment="Center" Height="445" Name="stpnlMenu">
<Button x:Name="btnCollapse" Width="15" Height="30" BorderBrush="{x:Null}" Foreground="{x:Null}" FlowDirection="RightToLeft" Margin="195,207.5,0,0" Click="BtnMenuShort_Click">
<Button.Background>
<ImageBrush ImageSource="/more_than_25px.png"/>
</Button.Background>
</Button>
<Button x:Name="btnMenu" Content="MENU" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-227.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" Click="BtnMenuShort_Click" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnHome" Content="HOME" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-187.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnFuel" Content="FUEL" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-147.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu" Click="BtnFuel_Click"/>
<Button x:Name="btnTyres" Content="TYRES" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-122.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnService" Content="SERVICE" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-97.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnBags" Content="WASTE BAGS" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-72.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnExpand" Width="15" Height="30" BorderBrush="{x:Null}" Foreground="{x:Null}" FlowDirection="LeftToRight" Margin="25,-30,0,0" Click="BtnMenuShort_Click" Visibility="Hidden">
<Button.Background>
<ImageBrush ImageSource="/more_than_25px.png"/>
</Button.Background>
</Button>
<Button x:Name="btnChat" Content="QUICK CHAT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,122.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnHelp" Content="SUPPORT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
<Button x:Name="btnLogOut" Content="LOG OUT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,5,0,20" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" Click="BtnLogOut_Click" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
</StackPanel>
</materialDesign:Card>
<StackPanel Width="30" Height="445" Margin="-400,155,0,0" Background="#0A1828">
<Rectangle Name="rec2" Height="10"/>
<Button Name="btnMenuShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Click="BtnMenuShort_Click" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/menu_30px.png" Stretch="Uniform"/>
</Button.Background>
</Button>
<Rectangle Name="rec3" Height="20"/>
<Button Name="btnHomeShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/home_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Rectangle Name="rec4" Height="20"/>
<Button Name="btnFuelShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand" Click="BtnFuel_Click">
<Button.Background>
<ImageBrush ImageSource="/gas_station_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Button Name="btnTyresShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/wheel_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Button Name="btnServiceShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/maintenance_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Button Name="btnBagsShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/waste_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Rectangle Name="rec5" Height="170"/>
<Button Name="btnChatShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/chat_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Button Name="btnHelpShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/help_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Button Name="btnLogOutShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Click="BtnLogOut_Click" Cursor="Hand">
<Button.Background>
<ImageBrush ImageSource="/exit_sign_30px.png" Stretch="Fill"/>
</Button.Background>
</Button>
<Rectangle Name="rec6" Height="10"/>
</StackPanel>
<materialDesign:Clock Name="clClock" Width="675" Height="22" FontFamily="Arial" FontWeight="SemiBold" FontSize="10" Foreground="White" Margin="0,578,0,0"/>
<Grid  Width="625" Height="550" Margin="25,25,25,25" Name="stackForms" Background="#FF292929">

</Grid>
</DockPanel>            

</materialDesign:Card>      
</materialDesign:Card>
</Window>

Form1(FuelDash(完美地加载到stackForms中,但现在我需要在单击BtnFuelLoad时将Form2(FuelEntry(加载到stackForms中。

Form1代码为:

using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using static SolAquaPro.WinHomeDash;
namespace SolAquaPro
{
public partial class FuelDash : Form
{
public FuelDash()
{
InitializeComponent();
}
private void Timer1_Tick(object sender, EventArgs e)
{
}
private void BtnFuelLoad_Click(object sender, EventArgs e)
{
this.Close();


}
private void BtnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

我知道我需要引用WinHomeDash中的对象,但我一直得到need to reference non static object.

Window Forms与GUI意义上的表单无关。这是一个平台。WPF是Windows窗体的继承者。

在WPF中,您扩展了Window而不是Form。只需删除对Windows窗体的所有引用即可
由于您正在尝试实现单页应用程序,因此您需要用Frame替换Grid。然后让您的所有页面扩展Page

WinHomeDash.xaml

<Window>
<StackPanel>
<!-- Navigation pane -->
<StackPanel Orientation="Horizontal">

<!-- Example how to navigate from outside the pages -->
<Button Click="LoadForm1_OnClick" Content="Form 1" />
<Button Click="LoadForm2_OnClick" Content="Form 2" />
</StackPanel>
<Frame x:Name="stackForms" 
NavigationUIVisibility="Hidden" />
</StackPanel>
</Window>

WinHomeDash.xaml.cs

partial class WinHomeDash : Window
{
private void LoadForm1_OnClick(object sender, RoutedEventArgs e)
{
Page form1 = new Form1();
this.stackForms.Navigate(form1);
}
private void LoadForm2_OnClick(object sender, RoutedEventArgs e)
{
Page form2 = new Form2();
this.stackForms.Navigate(form2);
}
}

然后在项目中添加一个新项,并选择Page。这将创建一个带有嵌套文件的.xml文件,该文件包含文件后面的部分类代码:

Form1.xaml

<Page>
<StackPanel>
<TextBlock Text="This is the first page." />
<!-- Example how to navigate from within the page -->
<Button Content="Navigate to next page" 
Click="LoadForm2_OnClick" />
</Page>

Form1.xaml.cs

public partial class Form1 : Page
{
public Form1()
{
InitializeComponent();
}
private void LoadForm2_OnClick(object sender, RoutedEventArgs e)
{
Page form2 = new Form2();
this.NavigationService.Navigate(form2);
}
}

Form2.xaml

<Page>
<StackPanel>
<TextBlock Text="This is the first page." />
<!-- Example how to navigate from within the page -->
<Button Content="Navigate to previous page" 
Click="LoadPreviousForm_OnClick" />
</Page>

Form2.xaml.cs

public partial class Form2 : Page
{
public Form1()
{
InitializeComponent();
}
private void LoadPreviousForm_OnClick(object sender, RoutedEventArgs e)
{    
// Navigate to previous page using the history
this.NavigationService.GoBack()
// Navigate to previous page using an explicit content
Page form1 = new Form1();
this.NavigationService.Navigate(form1);
}
}

由于Frame是一个heavyx控件,因为它还支持渲染web源,所以您应该使用ContentControl和DataTemplate来实现页面托管:
C#WPF页面之间导航(视图(
如何在WPF应用程序中的页面之间切换?

最新更新