WPF 从Main函数启动

代码:

/// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            Debug.WriteLine("App constructor");
            this.Startup += new StartupEventHandler(App_Startup);
        }

        void App_Startup(object sender, StartupEventArgs e)
        {
            Debug.WriteLine("App_Startup");
            MainWindow win = new MainWindow();
            this.MainWindow = win;
            win.Show();
        }
    }

    //<summary>
    //Entry point class to handle single instance of the application
    //</summary>
    public static class EntryPoint
    {
        [STAThread]
        public static void Main(string[] args)
        {
            Console.WriteLine("Main");
            Console.ReadLine();

            App app = new App();
            app.Run();
        }
    }


wpf 默认的Main函数在 app.g.cs文件中,如果我们想自己写个Main,也可以的。
首先,写好Main函数,如上面的代码所示

然后,有两个办法,

1.Project-->Properties-->Application-- Start Object 默认是No Set,更改为 WPFMainTest.EntryPoint就可以了。
2.App.xaml 右击选择Properties,Build Action 从ApplicationDefinition改为Page。

OK。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF (Windows Presentation Foundation) 是一种用于创建 Windows 桌面应用程序的技术,而 SQLite 则是一种轻量级的关系型数据库系统。EF (Entity Framework) 是一种对象关系映射 (ORM) 框架,使开发人员可以使用面向对象的代码来操作数据库。 在 WPF 应用程序中使用 SQLite 数据库,可以使用 EF 来简化数据访问过程。以下是在 WPF 应用程序中使用 SQLite 和 EF 的基本步骤: 1. 安装 SQLite 数据库和 EF。 2. 在 WPF 应用程序中添加对 EF 和 SQLite 的引用。 3. 创建一个 DbContext 类,该类继承自 DbContext 并包含对 SQLite 数据库的连接。 4. 在 DbContext 类中定义 Entity 类,这些类映射到 SQLite 数据库中的表。 5. 使用 DbSet 属性将 Entity 类添加到 DbContext 类中。 6. 在 WPF 应用程序中使用 DbContext 类来访问 SQLite 数据库。 示例代码: 首先,我们需要安装 SQLite 和 EF。可以使用 NuGet 包管理器来安装它们。 然后,我们创建一个 DbContext 类来连接 SQLite 数据库。以下是一个简单的 DbContext 类示例: ``` using System.Data.Entity; public class MyDbContext : DbContext { public MyDbContext() : base("name=MyConnectionString") { } public DbSet<Customer> Customers { get; set; } } ``` 在上面的代码中,我们定义了一个名为 MyDbContext 的 DbContext 类,并在构造函数中指定了连接字符串。然后,我们定义了一个 DbSet 属性,该属性表示与 Customer 表对应的 Entity 类。 接下来,我们需要定义一个 Entity 类来映射到 SQLite 数据库中的表。以下是一个简单的 Customer 类示例: ``` public class Customer { public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } } ``` 在上面的代码中,我们定义了一个名为 Customer 的 Entity 类,并包含三个属性:Id、Name 和 Address。 现在,我们可以在 WPF 应用程序中使用 MyDbContext 类来访问 SQLite 数据库。以下是一个简单的示例: ``` using (var context = new MyDbContext()) { var customer = new Customer { Name = "John Smith", Address = "123 Main St" }; context.Customers.Add(customer); context.SaveChanges(); } ``` 在上面的代码中,我们创建了一个新的 Customer 对象,并将其添加到 MyDbContext 类中的 Customers DbSet 属性中。然后,我们调用 SaveChanges 方法将更改保存到 SQLite 数据库中。 总结: 在 WPF 应用程序中使用 SQLite 和 EF 可以简化数据访问过程,使开发人员可以使用面向对象的代码来操作数据库。可以通过创建一个 DbContext 类和定义 Entity 类来实现这一点,然后在 WPF 应用程序中使用 DbContext 类来访问 SQLite 数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值