c#之Stylet开发文档 3 Bootstrapper引导程序

文章介绍了Bootstrapper在应用程序中的作用,包括配置IoC容器、创建ViewModel实例、窗口管理以及不同风格的Bootstrapper实现。同时提到了如何使用Stylet的内置IoC容器和自定义IoC容器的步骤。
摘要由CSDN通过智能技术生成

Bootstrapper引导程序

bootstrapper负责引导应用程序。它配置IoC容器,创建根ViewModel的新实例,并使用WindowManager显示它。它还提供各种其他功能,如下所述。

bootstrapper有两种风格:BootstrapperBase<TRootViewModel>,它需要您自己配置IoC容器,以及Bootstrapper<TRootViewModel>(使用Stylet的内置IoC容器StyletIoC)。

使用StyletIoC的引导程序示例:

class Bootstrapper : Bootstrapper<MyRootViewModel>
{
   protected override void OnStart()
   {
      // This is called just after the application is started, but before the IoC container is set up.
      // Set up things like logging, etc
   }
 
   protected override void ConfigureIoC(IStyletIoCBuilder builder)
   {
      // Bind your own types. Concrete types are automatically self-bound.
      builder.Bind<IMyInterface>().To<MyType>();
   }
 
   protected override void Configure()
   {
      // This is called after Stylet has created the IoC container, so this.Container exists, but before the
      // Root ViewModel is launched.
      // Configure your services, etc, in here
   }
 
   protected override void OnLaunch()
   {
      // This is called just after the root ViewModel has been launched
      // Something like a version check that displays a dialog might be launched from here
   }
 
   protected override void OnExit(ExitEventArgs e)
   {
      // Called on Application.Exit
   }
 
   protected override void OnUnhandledException(DispatcherUnhandledExceptionEventArgs e)
   {
      // Called on Application.DispatcherUnhandledException
   }
}

使用自定义IoC容器

将另一个IoC容器与Stylet一起使用很容易。我在Bootstrappers项目中包含了许多流行的IoC容器的Bootstrapper。这些都经过了单元测试,但没有经过战斗测试:请随意定制。

请注意,Stylet-nuget包/dll不包括这些,因为它会添加不必要的依赖项。类似地,我不发布IoC容器特定的包,因为这是浪费精力。

将你想要的引导程序从上面的链接复制到你的项目中。然后将其子类化,就像您通常将Bootstrapper<TRootViewModel>子类化一样,如上所述。然后将您的子类添加到App.xaml.cs中,如快速入门中所述,例如。

public class Bootstrapper : AutofacBootstrapper<ShellViewModel>
{
}
<Application x:Class="Stylet.Samples.Hello.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:s="https://github.com/canton7/Stylet"
             xmlns:local="clr-namespace:Stylet.Samples.Hello">
    <Application.Resources>
       <s:ApplicationLoader>
            <s:ApplicationLoader.Bootstrapper>
                <local:Bootstrapper/>
            </s:ApplicationLoader.Bootstrapper>
        </s:ApplicationLoader>
    </Application.Resources>
</Application>

如果你想为另一个IoC容器编写自己的引导程序,那也很容易。看看上面的引导程序,看看你需要做什么。

将资源词典添加到App.xaml

s: ApplicationLoader本身就是一个ResourceDictionary。如果您需要将自己的资源字典添加到App.xaml,则必须在ResourceDictionary中嵌套s:ApplicationLoader作为MergedDictionary,如下所示:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <s:ApplicationLoader>
                <s:ApplicationLoader.Bootstrapper>
                    <local:Bootstrapper/>
                </s:ApplicationLoader.Bootstrapper>
            </s:ApplicationLoader>

            <ResourceDictionary Source="YourDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
WPF(Windows Presentation Foundation)是一种用于创建Windows应用程序的UI框架,而Stylet是一个轻量级的MVVM(Model-View-ViewModel)框架,提供了一种组织和管理应用程序代码的方式。SQLite是一种嵌入式数据库引擎,可以用于存储和管理数据。 在基于Stylet框架的WPF应用程序中使用SQLite进行增删改查操作,可以按照以下步骤进行: 1. 添加SQLite NuGet包:在Visual Studio中,右击项目名称,选择"管理NuGet程序包"。在NuGet包管理器中搜索并安装SQLite包。 2. 创建SQLite数据库连接:在代码中使用SQLiteConnection对象创建与数据库的连接。可以指定数据库文件的路径,如果数据库不存在,会自动创建。 3. 定义表结构和数据模型:使用SQLite提供的SQL语句在数据库中创建表,并定义与之对应的数据模型类。可以使用类属性来映射表的字段。 4. 实现增删改查功能:在ViewModel中,使用SQLiteConnection对象进行增删改查操作。可以编写SQL语句,也可以使用SQLite提供的ORM工具(如Dapper)进行操作。 示例代码如下: ```csharp using Stylet; using Dapper; using System.Collections.Generic; using System.Data.SQLite; using System.Linq; namespace MyNamespace { public class MyViewModel : Screen { private SQLiteConnection connection; // 构造函数中初始化SQLite连接 public MyViewModel() { string databasePath = "path_to_your_database_file"; connection = new SQLiteConnection($"Data Source={databasePath};Version=3;"); } // 查询操作 public IEnumerable<MyDataModel> GetMyData() { string sql = "SELECT * FROM MyTable"; return connection.Query<MyDataModel>(sql); } // 插入操作 public void InsertData(MyDataModel data) { string sql = "INSERT INTO MyTable (Name, Age) VALUES (@Name, @Age)"; connection.Execute(sql, data); } // 更新操作 public void UpdateData(MyDataModel data) { string sql = "UPDATE MyTable SET Name = @Name, Age = @Age WHERE Id = @Id"; connection.Execute(sql, data); } // 删除操作 public void DeleteData(int id) { string sql = "DELETE FROM MyTable WHERE Id = @Id"; connection.Execute(sql, new { Id = id }); } } public class MyDataModel { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } } ``` 以上是基于Stylet框架的WPF应用程序使用SQLite进行增删改查操作的简单示例。根据实际需求,可以根据这个示例进行扩展和修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值