prism中依赖注入的使用
BlankApp\BlankApp\App.xaml.cs
| |
| using System.Windows; |
| using BlankApp.Views; |
| using Prism.Ioc; |
| |
| namespace BlankApp |
| { |
| |
| |
| |
| public partial class App |
| { |
| protected override Window CreateShell() |
| { |
| return Container.Resolve<MainWindow>(); |
| } |
| protected override void RegisterTypes(IContainerRegistry containerRegistry) |
| { |
| |
| |
| |
| 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 |
| { |
| |
| public interface IMyService |
| { |
| string GetMessage(); |
| } |
| |
| |
| 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> |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
2024-01-10 如何写简历
2023-01-10 点8的python的pip包