怪奇物语

怪奇物语

首页 新随笔 联系 管理

prism中依赖注入的使用

BlankApp\BlankApp\App.xaml.cs


using System.Windows;
using BlankApp.Views;
using Prism.Ioc;

namespace BlankApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

            //👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇
            // 注册IMyService接口与MyService实现之间的映射
            containerRegistry.Register<IMyService, MyService>();
        }
    }
}

BlankApp\BlankApp\BlankApp.csproj


<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Prism.DryIoc" Version="8.1.97" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Services\" />
  </ItemGroup>
</Project>

BlankApp\BlankApp\BlankApp.csproj.user


<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup />
  <ItemGroup>
    <ApplicationDefinition Update="App.xaml">
      <SubType>Designer</SubType>
    </ApplicationDefinition>
  </ItemGroup>
  <ItemGroup>
    <Page Update="Views\MainWindow.xaml">
      <SubType>Designer</SubType>
    </Page>
  </ItemGroup>
</Project>

BlankApp\BlankApp\MyService.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlankApp
{
    // IMyService.cs
    public interface IMyService
    {
        string GetMessage();
    }

    // MyService.cs
    public class MyService : IMyService
    {
        public string GetMessage()
        {
            return "Hello from MyService!";
        }
    }
}

BlankApp\BlankApp\ViewModels\MainWindowViewModel.cs


using Prism.Mvvm;

namespace BlankApp.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Application";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        // 👇👇👇👇👇👇👇👇👇👇👇👇👇👇
        private readonly IMyService _myService;

                                // 👇👇👇👇👇👇👇👇
        public MainWindowViewModel(IMyService myService)
        {
            _myService = myService;
            this.InitTile();
        }
        private void InitTile()
        {
            Title = _myService.GetMessage();
        }
    }
}

BlankApp\BlankApp\Views\MainWindow.xaml


<Window x:Class="BlankApp.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
                //👇👇👇👇
        Title="{Binding Title}" Height="350" Width="525" >
    <Grid>
        <ContentControl prism:RegionManager.RegionName="ContentRegion" />
    </Grid>
</Window>

posted on 2025-01-10 08:00  超级无敌美少男战士  阅读(58)  评论(0)    收藏  举报