首页 > 解决方案 > 如何在 C# 中打开新窗口而不丢失 xml 布局

问题描述

我有一个 ac# 项目已经在一个主窗口工作,但是当我创建一个新窗口并使用

Views.Form Form = new Views.Form();
Form.ShowDialog();

该页面已打开,但未显示我设计的 xml,我认为问题出在名称空间和目录中,但很难解决问题,所以这是我的解决方案资源管理器

   Properities
   References
   Model
     Acteur.cs
     Tache.cs
   ViewModels
     ActeurViewModel.cs
     TacheViewModel.cs
   Views
     Form.xaml
     MainWindow.xaml
   App.xaml
   Model.edmx

这是窗体窗口代码:

.cs 文件

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;

namespace WpfApplication2.Views
{
    /// <summary>
    /// Interaction logic for Form.xaml
    /// </summary>
    public partial class Form : Window
    {
        public Form()
        {
            
        }
    }
}

.xaml 文件

<Window x:Class="WpfApplication2.Form"
        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:WpfApplication2"
        mc:Ignorable="d"
        Title="Form" Height="300" Width="300">
    <Grid Background="#FF142683">
        <Label  Name="Test" Content="this is a test" FontSize="43"/>
    </Grid>
</Window>

MainWindow 调用

private void AddButtonClicked(object sender, RoutedEventArgs e)
        {
            Views.Form Form = new Views.Form();
            Form.ShowDialog();
        }

标签: c#xamlnew-window

解决方案


有没有删除方法“ InitializeComponent ();” 在构造函数中?

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }
}

此方法由 vs 自动创建,它将初始化您在 XAML.cs 中使用的那些控件。

由于您已删除该方法,这就是您看不到控件的原因。

只需尝试创建一个新窗口,您就会找到我提到的方法。


推荐阅读