【问题标题】:Is there a way to show Form2 on MainWindow from button click on Form1有没有办法通过单击 Form1 按钮在 MainWindow 上显示 Form2
【发布时间】:2021-12-23 12:39:51
【问题描述】:

我已设法让 Form1 在 MainWindow 的网格中打开,但现在我需要在单击 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.

【问题讨论】:

  • 你真的需要混合 WPF 和 Winforms 吗?只是问问而已。您可以从 FuelDash 发布事件并在 WinHomeDash 中对其做出反应。
  • 如果您正在编写一个新应用程序,我也想知道为什么不只选择一种 ui 技术。
  • 所以当我创建这个应用程序而不是右键单击“参考”->“添加新”->“窗口(WPF)”->“Windows 窗体”->“窗体(Windows Forms)' 每次我想进入新的仪表板或条目表时,我应该添加什么?
  • WPF 不是 windows 窗体。两种不同的东西。不要添加对 Windows 窗体的任何引用。使用新的 wpf 窗口。或者在单个窗口应用程序的一个 wpf 主窗口中托管新的 wpf 用户控件。

标签: c# wpf wpf-controls


【解决方案1】:

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

在 WPF 中,您扩展 Window 而不是 Form。只需删除对 Windows 窗体的所有引用。
由于您正在尝试实现单页应用程序,因此您可以将Grid 替换为Frame。然后让你所有的页面扩展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。这将创建一个 .xaml 文件,其中包含一个包含部分类代码隐藏文件的嵌套文件:

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 是一个重型控件,因为它还支持呈现 Web 源,您应该使用 ContentControl 和 DataTemplate 来实现页面托管:
C# WPF Navigation Between Pages (Views)
How do I toggle between pages in a WPF application?

【讨论】:

  • 非常感谢,我会从头开始重做应用程序,然后实现你所说的。非常感谢。
  • 是的,你真的应该从头开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
相关资源
最近更新 更多