iconDreamine
← 목록

Dreamine.MVVM.Locators.Wpf

stablev1.0.7

WPF DataTemplate 자동 연결 + DesignTime 지원 ViewModel Locator.

#dreamine#injection#locator#mvvm#viewmodel#wpf
TFM net8.0-windowsPackage Dreamine.MVVM.Locators.Wpf참조 Dreamine.MVVM.Interfaces, Dreamine.MVVM.Locators

Dreamine.MVVM.Locators.Wpf

Dreamine MVVM Locator 시스템의 WPF 통합 레이어입니다.

이 패키지는 Dreamine.MVVM.Locators를 WPF 런타임에 연결하기 위한 기능을 제공합니다.

주요 역할:

  • View ↔ ViewModel 자동 바인딩
  • ViewModel 기반 View 동적 생성
  • Region 기반 ContentControl 네비게이션
  • ContentControl 전용 네비게이터 제공

무거운 프레임워크 없이 WPF 환경에서 Locator 기반 구성을 적용하려는 경우를 위한 패키지입니다.

➡️ English Version


목적

Dreamine.MVVM.Locators.Wpf는 다음 요소를 연결하는 WPF 런타임 브리지입니다.

  • Dreamine.MVVM.Locators
  • WPF FrameworkElement
  • XAML Attached Property
  • Region / ContentControl 네비게이션 패턴

즉, WPF 애플리케이션에서 View와 ViewModel을 실제 런타임에 연결하는 역할을 담당합니다.


주요 구성 요소

ViewModelBinder

WPF AttachedProperty 기반 ViewModel 자동 연결 헬퍼입니다.

역할:

  • AutoWireViewModel AttachedProperty 제공
  • ViewModelLocator.Resolve(view.GetType())를 통해 ViewModel 생성
  • 생성된 ViewModel을 DataContext에 할당
  • ViewModel 인스턴스로부터 View를 네이밍 규칙 기반으로 생성

예시:

<Window
    xmlns:locator="clr-namespace:Dreamine.MVVM.Locators.Wpf"
    locator:ViewModelBinder.AutoWireViewModel="True">
</Window>

RegionBinder

ContentControl을 Region 이름으로 등록하고 탐색하는 AttachedProperty 바인더입니다.

역할:

  • Region 이름 기준 ContentControl 등록
  • Region 키로 탐색 수행
  • ViewModelBinder.ResolveView를 사용해 View 생성 후 표시
  • region 참조를 weak reference 로 유지하고 control 이 unload 되거나 이름이 바뀌면 등록 해제

예시:

<ContentControl
    local:RegionBinder.RegionName="MainRegion" />
RegionBinder.Navigate("MainRegion", viewModel);

RegionBinder는 등록된 ContentControl을 강하게 참조하지 않습니다. 따라서 닫힌 Window 또는 unload 된 control 이 static region registry 때문에 계속 살아남는 일을 방지합니다.


ContentControlNavigator

ContentControl 기반의 단순한 INavigator 구현체입니다.

역할:

  • ViewModel에 대응하는 View 생성
  • 생성한 View를 ContentControl.Content에 설정

예시:

var navigator = new ContentControlNavigator(MainContentControl);
navigator.Navigate(viewModel);

RegionBinderHelper

RegionBinder.RegionName 기준으로 ContentControl을 찾는 VisualTree helper 입니다.

역할:

  • WPF root element 하위 visual tree 검색
  • 일치하는 region control 반환
  • root 가 null 이면 ArgumentNullException 발생
  • region 이름이 비어 있으면 ArgumentException 발생

예시:

ContentControl? region = RegionBinderHelper.FindRegionControl(window, "MainRegion");

View 해석 규칙

ViewModelBinder.ResolveView(object viewModel)는 여러 네이밍 규칙을 순차적으로 시도합니다.

지원되는 대표 규칙:

  • ViewModelsViews
  • ViewModelView
  • ViewModel 제거
  • ViewModelsPages
  • PageModelsPages
  • PageModelPage

따라서 WPF 프로젝트마다 조금씩 다른 네이밍 스타일도 수용할 수 있습니다.


런타임 참고 사항

  • WPF helper 는 FrameworkElement를 생성하고 갱신하므로 UI thread 에서 호출하는 것을 전제로 합니다.
  • 라이브러리 예외 메시지는 로그/콘솔 파싱을 위해 이모지 없는 일반 텍스트를 사용합니다.
  • 등록되지 않은 region 탐색은 region 이름을 포함한 InvalidOperationException을 발생시킵니다.

요구사항

  • .NET 8.0
  • WPF 사용 설정
  • Dreamine.MVVM.Locators 참조 필요

설치

dotnet add package Dreamine.MVVM.Locators.Wpf

또는 프로젝트 파일에 직접 추가:

<PackageReference Include="Dreamine.MVVM.Locators.Wpf" Version="1.0.3" />

아키텍처 위치

이 패키지는 Dreamine MVVM 스택의 WPF 통합 레이어에 해당합니다.

Dreamine.MVVM.Locators
        ↓
Dreamine.MVVM.Locators.Wpf
        ↓
WPF Views / Regions / Navigation

사용 대상

다음 경우에 사용하면 됩니다.

  • Dreamine MVVM 기반 WPF 애플리케이션을 만들 때
  • XAML에서 ViewModel 자동 바인딩이 필요할 때
  • ViewModel로부터 View를 동적으로 생성해야 할 때
  • 대형 프레임워크 없이 가벼운 Region / Content 네비게이션이 필요할 때

WPF 외 환경에서는 사용 대상이 아닙니다.


License

MIT License

구조 다이어그램

classDiagram
    class WpfViewModelLocator {
        -IServiceProvider _provider
        +Locate~T~() T
        +Locate(Type) object
        +SetProvider(IServiceProvider) void
    }
    class ViewModelLocatorSource {
        <<static>>
        +Design bool
        +SetServiceProvider(IServiceProvider) void
        +Get~T~() T
    }
    class WpfAutoWireExtension {
        <<static>>
        +GetAutoWireViewModel(DependencyObject) bool
        +SetAutoWireViewModel(DependencyObject, bool) void
        -OnAutoWireViewModelChanged(DependencyObject, DependencyPropertyChangedEventArgs) void
    }
    class IViewModelLocator {
        <<interface>>
    }
    IViewModelLocator <|.. WpfViewModelLocator
    WpfViewModelLocator <-- ViewModelLocatorSource
    WpfAutoWireExtension --> WpfViewModelLocator

API 문서

타입

ContentControlNavigator

\if KO 에 ViewModel 대응 View를 표시하는 기본 네비게이터입니다. \endif \if EN Provides basic navigation by displaying the View corresponding to a ViewModel in a . \endif

RegionBinder

\if KO 이름을 기준으로 영역을 관리합니다. \endif \if EN Manages regions by name. \endif

RegionBinderHelper

\if KO 를 통해 등록된 WPF 영역 컨트롤을 찾습니다. \endif \if EN Finds WPF region controls registered through . \endif

ViewModelBinder

\if KO ViewModel 자동 바인딩과 View 확인을 위한 WPF 도우미를 제공합니다. \endif \if EN Provides WPF helpers for automatic ViewModel binding and View resolution. \endif

ContentControlNavigator

#ctor Method

\if KO 지정한 콘텐츠 컨트롤을 대상으로 네비게이터를 초기화합니다. \endif \if EN Initializes the navigator with the specified target content control. \endif

target— \if KO 탐색 결과를 표시할 컨트롤입니다. \endif \if EN The control that displays navigation results. \endif
Navigate Method

\if KO ViewModel에 대응하는 View를 확인하여 대상 컨트롤에 표시합니다. \endif \if EN Resolves the View corresponding to a ViewModel and displays it in the target control. \endif

viewModel— \if KO 표시할 ViewModel 인스턴스입니다. \endif \if EN The ViewModel instance to display. \endif
_target Field

\if KO target 값을 보관합니다. \endif \if EN Stores the target value. \endif

RegionBinder

Clear Method

\if KO 등록된 모든 영역을 지웁니다. \endif \if EN Clears all registered regions. \endif

GetRegionName Method

\if KO 종속성 객체에 설정된 영역 이름을 가져옵니다. \endif \if EN Gets the region name assigned to a dependency object. \endif

obj— \if KO 대상 종속성 객체입니다. \endif \if EN The target dependency object. \endif

반환: \if KO 설정된 영역 이름입니다. \endif \if EN The assigned region name. \endif

Navigate Method

\if KO 지정한 영역에 ViewModel에 대응하는 View를 표시합니다. \endif \if EN Displays the View corresponding to a ViewModel in the specified region. \endif

regionName— \if KO 대상 영역 이름입니다. \endif \if EN The target region name. \endif
viewModel— \if KO 표시할 ViewModel입니다. \endif \if EN The ViewModel to display. \endif
OnRegionLoaded Method

\if KO 영역 컨트롤이 로드되면 다시 등록합니다. \endif \if EN Re-registers a region control when it is loaded. \endif

sender— \if KO 로드된 객체입니다. \endif \if EN The loaded object. \endif
e— \if KO 라우트 이벤트 정보입니다. \endif \if EN The routed event data. \endif
OnRegionNameChanged Method

\if KO 영역 이름 변경에 따라 컨트롤 등록과 수명 주기 이벤트 구독을 갱신합니다. \endif \if EN Updates control registration and lifetime event subscriptions after a region-name change. \endif

d— \if KO 변경된 종속성 객체입니다. \endif \if EN The changed dependency object. \endif
e— \if KO 변경 이벤트 정보입니다. \endif \if EN The change event data. \endif
OnRegionUnloaded Method

\if KO 영역 컨트롤이 언로드되면 등록을 해제합니다. \endif \if EN Unregisters a region control when it is unloaded. \endif

sender— \if KO 언로드된 객체입니다. \endif \if EN The unloaded object. \endif
e— \if KO 라우트 이벤트 정보입니다. \endif \if EN The routed event data. \endif
Register Method

\if KO 영역 이름과 컨트롤의 약한 참조를 등록합니다. \endif \if EN Registers a weak reference from a region name to a control. \endif

regionName— \if KO 영역 이름입니다. \endif \if EN The region name. \endif
control— \if KO 등록할 컨트롤입니다. \endif \if EN The control to register. \endif
SetRegionName Method

\if KO 종속성 객체에 영역 이름을 설정합니다. \endif \if EN Sets the region name on a dependency object. \endif

obj— \if KO 대상 종속성 객체입니다. \endif \if EN The target dependency object. \endif
value— \if KO 설정할 영역 이름입니다. \endif \if EN The region name to set. \endif
TryGetRegion Method

\if KO 살아 있는 등록 컨트롤을 영역 이름으로 가져옵니다. \endif \if EN Gets a live registered control by region name. \endif

regionName— \if KO 찾을 영역 이름입니다. \endif \if EN The region name to find. \endif
control— \if KO 찾은 컨트롤입니다. \endif \if EN The located control. \endif

반환: \if KO 살아 있는 컨트롤을 찾았으면 입니다. \endif \if EN when a live control is found. \endif

Unregister Method

\if KO 현재 등록된 컨트롤이 일치하면 영역 등록을 제거합니다. \endif \if EN Removes a region registration when its current control matches. \endif

regionName— \if KO 영역 이름입니다. \endif \if EN The region name. \endif
control— \if KO 등록 해제할 컨트롤입니다. \endif \if EN The control to unregister. \endif
RegionMap Field

\if KO Region Map 값을 보관합니다. \endif \if EN Stores the region map value. \endif

RegionNameProperty Field

\if KO Region Name Property 값을 보관합니다. \endif \if EN Stores the region name property value. \endif

SyncRoot Field

\if KO Sync Root 값을 보관합니다. \endif \if EN Stores the sync root value. \endif

RegionBinderHelper

FindRegionControl Method

\if KO 시각적 트리에서 지정한 영역 이름의 을 재귀적으로 찾습니다. \endif \if EN Recursively searches the visual tree for a with the specified region name. \endif

root— \if KO 검색을 시작할 루트 시각적 객체입니다. \endif \if EN The root visual to search from. \endif
regionName— \if KO 찾을 영역 이름입니다. \endif \if EN The region name to find. \endif

반환: \if KO 일치하는 컨트롤이며, 없으면 입니다. \endif \if EN The matching content control, or when none exists. \endif

ViewModelBinder

FindViewType Method

\if KO 로드된 어셈블리에서 생성 가능한 WPF View 후보 타입을 찾습니다. \endif \if EN Finds a creatable WPF View candidate type in the loaded assemblies. \endif

candidateTypeNames— \if KO 검색할 전체 타입 이름 후보입니다. \endif \if EN The full type-name candidates to search. \endif

반환: \if KO 찾은 View 타입이며, 없으면 입니다. \endif \if EN The located View type, or when none is found. \endif

GetAutoWireViewModel Method

\if KO ViewModel 자동 바인딩 활성화 여부를 가져옵니다. \endif \if EN Gets whether automatic ViewModel binding is enabled. \endif

obj— \if KO 대상 종속성 객체입니다. \endif \if EN The target dependency object. \endif

반환: \if KO 자동 바인딩이 활성화되었으면 입니다. \endif \if EN when automatic binding is enabled. \endif

OnAutoWireViewModelChanged Method

\if KO 자동 연결 속성이 활성화되면 ViewModel을 확인해 DataContext에 할당합니다. \endif \if EN Resolves and assigns a ViewModel when the auto-wire property is enabled. \endif

d— \if KO 변경된 종속성 객체입니다. \endif \if EN The changed dependency object. \endif
e— \if KO 속성 변경 정보입니다. \endif \if EN The property change data. \endif
ResolveView Method

\if KO 지정한 ViewModel에 대응하는 View를 확인하고 DataContext에 ViewModel을 할당합니다. \endif \if EN Resolves the View corresponding to the specified ViewModel and assigns it as DataContext. \endif

viewModel— \if KO ViewModel 인스턴스입니다. \endif \if EN The ViewModel instance. \endif

반환: \if KO 확인 및 생성된 View 인스턴스입니다. \endif \if EN The resolved and created View instance. \endif

SetAutoWireViewModel Method

\if KO ViewModel 자동 바인딩 활성화 여부를 설정합니다. \endif \if EN Sets whether automatic ViewModel binding is enabled. \endif

obj— \if KO 대상 종속성 객체입니다. \endif \if EN The target dependency object. \endif
value— \if KO 자동 확인 및 할당을 활성화하려면 입니다. \endif \if EN to resolve and assign a ViewModel automatically. \endif
AutoWireViewModelProperty Field

\if KO View의 DataContext에 ViewModel을 자동 할당하는 연결 속성입니다. \endif \if EN Gets the attached property used to assign a ViewModel to a View DataContext automatically. \endif