就想我这样 发表于 2024-4-17 11:22:45

wpf程序修改启动入口注意事项

如果你在自定义的 Main 方法中直接使用 App 类并启动应用程序,但发现 App.xaml 中定义的资源没有被正确加载,那么问题可能在于如何正确配置 App.xaml 与你的 App 类的交互。
确保 App.xaml 文件中的 x:Class 属性正确指向你的 App 类。这样,当你创建 App 类的实例并调用 Run 方法时,它会自动处理在 App.xaml 中定义的资源和配置。
步骤 1: 检查 App.xaml 设置

首先,确保 App.xaml 的 Build Action 设置为 ApplicationDefinition,并且 x:Class 属性指向你的 App 类全名,如下:
<Application x:
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:YourNamespace">
    <Application.Resources>
      
    </Application.Resources>
</Application>步骤 2: 修改 App 类

确保你的 App 类与 App.xaml 文件关联,并且在 App.xaml.cs 中没有覆盖默认的资源加载逻辑。通常,你不需要在 App.xaml.cs 中手动加载这些资源,因为它们会通过 App.xaml 的声明自动处理。
using System.Windows;

namespace YourNamespace
{
    public partial class App : Application
    {
      // 其他代码(如果有)
    }
}步骤 3: 检查 Main 方法

你的 Main 方法应该看起来像这样,确保只是创建 App 类的实例并启动它:

public static void Main()
{
    App app = new App();
    app.Run();
}步骤 4: 确认项目配置

在工程设置那里,修改对应类文件的启动对象
步骤 5:App类对象初始化

一定要调用InitializeComponent();
public partial class App : Application
{
        public App()
        {
                InitializeComponent();
        }
}
来源:https://www.cnblogs.com/c9080/p/18139992
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: wpf程序修改启动入口注意事项