怪物奇妙物语

宇宙无敌超级美少男的怪物奇妙物语

首页 新随笔 联系 管理
  819 随笔 :: 0 文章 :: 2 评论 :: 16万 阅读

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   超级无敌美少男战士  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
历史上的今天:
2024-01-10 如何写简历
2023-01-10 点8的python的pip包
点击右上角即可分享
微信分享提示