You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用函数打开Main_win.xaml时WPF窗口无内容显示求助

WPF窗口无内容显示问题排查

问题

WPF开发中,通过Application_Startup函数打开Main_win.xaml窗口时,窗口无法显示任何控件(包括按钮、Ribbon等),添加新控件也无效果。

相关代码

App.xaml

<Application x:Class="test.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:salesandwarehousingsystem"
         ShutdownMode="OnExplicitShutdown" 
         Startup  = "Application_Startup" >
</Application>

App.xaml.cs

private void Application_Startup(object sender, StartupEventArgs e)
{
    Main_win main_Win = new Main_win();
    main_Win.ShowDialog();
}

Main_win.xaml

<Window x:Class="test.window.Main_win"
    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:salesandwarehousingsystem.window"
    xmlns:custom="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" 
    mc:Ignorable="d"
    Title="Main_win" Height="660" Width="1300">
<Grid>
    
    <Ribbon >
        <Ribbon.QuickAccessToolBar>
            <RibbonQuickAccessToolBar >
                <RibbonSplitButton  >
                    <RibbonSplitMenuItem Header=" About Us"/>
                    <RibbonSplitMenuItem Header=" Contact Us"/>
                    <RibbonSplitMenuItem Header=" Exit"/>
                </RibbonSplitButton>
            </RibbonQuickAccessToolBar>
        </Ribbon.QuickAccessToolBar>
        <Button Content="Button" Height="21" Width="136"/>
    </Ribbon>
    <Button Content="Button" Margin="561,282,422,282"/>

</Grid>
</Window>

运行效果

空白的Main_win窗口

解决思路

  1. 统一命名空间
    App.xaml中x:Class为test.App,但xmlns:local指向salesandwarehousingsystem;Main_win.xaml中x:Class为test.window.Main_win,xmlns:local指向salesandwarehousingsystem.window,命名空间完全混乱。需将项目的代码文件命名空间与XAML中的x:Class、xmlns:local统一,例如:

    • 将App.xaml的x:Class改为salesandwarehousingsystem.App
    • 将Main_win.xaml的x:Class改为salesandwarehousingsystem.window.Main_win
    • 确保对应的cs文件中namespace声明与XAML一致
  2. 检查XAML编译设置
    右键项目→属性→生成,确认所有XAML文件的生成操作为Page,这是WPF正确加载XAML界面的必要条件。

  3. 确保初始化方法被调用
    检查Main_win.xaml.cs的构造函数,确认存在InitializeComponent()调用,该方法负责加载XAML中定义的界面元素,缺失会导致窗口空白:

    public Main_win()
    {
        InitializeComponent();
    }
    

内容的提问来源于stack exchange,提问作者Hossein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 03:35:22