WPF教程(十)使用App.xaml

App.xaml是应用的声明起始点。在VS新建一个WPF应用,就能自动生成一个App.xaml,同时包含了后台代码文件App.xaml.cs。这两个文件都是局部类,和Window类非常相似,让你能够使用标记语言和后台代码。

App.xaml.cs扩展了应用类,它是WPF窗口应用的中心类。.NET首先进入这个类的起始指令,从这里启动预想的窗口或者网页。同时这里订阅了重要的应用事件,如应用启动、未处理的异常等等。

App.xaml最常使用的特性是定义全局资源,它们可能会在整个应用里面被使用或者访问,如全局样式。

App.xaml结构

自动生成的App.xaml代码如下:

<Application x:Class="WpfTutorialSamples.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>
主要来看StartupUri属性,它指示了启动应用的时候,加载哪个窗口或网页。在这里将加载MainWindow.xaml。可以通过替换来使用不同的窗体。

有时候你想获取更多的控制,如窗口如何显示,什么时候显示。其实可以删去StartupUri及其值,然后在后台代码里实现。后面会讲。

App.xaml.cs结构

对应的代码如下:
<span style="font-size:14px;">using System;
using System.Collections.Generic;
using System.Windows;

namespace WpfTutorialSamples
{
        public partial class App : Application
        {

        }
}</span>
从上面可以看到这个类是如何扩展应用类的,它允许我们在应用层填充内容。例如,可以订阅启动事件,用来指定启动窗口。
看下面的例子:
<span style="font-size:14px;"><Application x:Class="WpfTutorialSamples.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                         Startup="Application_Startup">
    <Application.Resources></Application.Resources>
</Application></span>
StartupUri被一个启动事件的订阅所取代(通过XAML订阅事件会在后面将到)。在后台代码使用事件,如下:
<span style="font-size:14px;">using System;
using System.Collections.Generic;
using System.Windows;

namespace WpfTutorialSamples
{
        public partial class App : Application
        {

                private void Application_Startup(object sender, StartupEventArgs e)
                {
                        // Create the startup window
                        MainWindow wnd = new MainWindow();
                        // Do stuff here, e.g. to the window
                        wnd.Title = "Something else";
                        // Show the window
                        wnd.Show();
                }
        }
}</span>
与前面使用StartupUri属性相比,这里可以在显示启动窗口之前对它进行操作,例如改变标题。尽管不是很常用,这可以订阅事件,或者显示一个启动界面。当你拥有所有的控制权,就有了很多种可能。在后面的章节我们会深入研究。




  • 13
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
WPF (Windows Presentation Foundation) 是一个基于 .NET Framework 的 UI 框架,它提供了丰富的界面设计和开发功能。在 WPF使用 Microsoft.Extensions.DependencyInjection 可以帮助我们更好地实现依赖注入(Dependency Injection)。 Microsoft.Extensions.DependencyInjection 是 .NET Core 中的一个轻量级依赖注入框架,它提供了简单、灵活的依赖注入方式。在 WPF 中,我们同样可以使用该框架来实现依赖注入。 首先,我们需要在 WPF 应用程序中安装 Microsoft.Extensions.DependencyInjection NuGet 包。 在 App.xaml.cs 文件中,我们可以创建一个 IServiceCollection 对象,并在其中注册依赖: ```csharp public partial class App : Application { public static IServiceProvider ServiceProvider { get; private set; } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var services = new ServiceCollection(); // 注册依赖 services.AddSingleton<IMyService, MyService>(); // 创建容器 ServiceProvider = services.BuildServiceProvider(); } } ``` 在上面的代码中,我们注册了一个名为 `MyService` 的服务,并指定其实现接口为 `IMyService`。当我们需要使用该服务的时候,可以通过 ServiceProvider 来解析: ```csharp var service = App.ServiceProvider.GetService<IMyService>(); ``` 这样就完成了依赖注入的过程。在 WPF 中,我们通常会在 ViewModel 中使用依赖注入,来实现视图和逻辑的分离。通过使用 Microsoft.Extensions.DependencyInjection,我们可以更方便地管理和注入依赖项,提高代码的可测试性和可维护性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值