Dreamine.MVVM.Locators.Wpf
1.0.7
Dreamine.MVVM.Locators.Wpf 사용자 인터페이스 기능과 구성 요소를 제공합니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
ViewModelBinder.cs
이 파일의 문서화 페이지로 가기
1
using
System;
2
using
System.Windows;
3
using
Dreamine.MVVM.Locators
;
4
5
namespace
Dreamine.MVVM.Locators.Wpf
6
{
15
public
static
class
ViewModelBinder
16
{
25
public
static
readonly DependencyProperty
AutoWireViewModelProperty
=
26
DependencyProperty.RegisterAttached(
27
"AutoWireViewModel"
,
28
typeof(
bool
),
29
typeof(
ViewModelBinder
),
30
new
PropertyMetadata(
false
,
OnAutoWireViewModelChanged
));
31
56
public
static
bool
GetAutoWireViewModel
(DependencyObject obj)
57
{
58
return
(
bool
)obj.GetValue(
AutoWireViewModelProperty
);
59
}
60
85
public
static
void
SetAutoWireViewModel
(DependencyObject obj,
bool
value)
86
{
87
obj.SetValue(
AutoWireViewModelProperty
, value);
88
}
89
130
public
static
FrameworkElement
ResolveView
(
object
viewModel)
131
{
132
if
(viewModel is
null
)
133
{
134
throw
new
ArgumentNullException(nameof(viewModel));
135
}
136
137
Type viewModelType = viewModel.GetType();
138
string
fullName = viewModelType.FullName
139
??
throw
new
InvalidOperationException($
"Cannot determine the full type name for {viewModelType.Name}."
);
140
141
string
[] candidates = ViewNamingConvention.GetViewTypeNameCandidates(fullName);
142
Type? viewType =
FindViewType
(candidates);
143
144
if
(viewType is
null
)
145
{
146
throw
new
InvalidOperationException($
"Cannot find a View for candidates: {string.Join("
or
", candidates)}."
);
147
}
148
149
FrameworkElement view = (FrameworkElement)Activator.CreateInstance(viewType)!;
150
view.DataContext = viewModel;
151
152
return
view;
153
}
154
179
private
static
void
OnAutoWireViewModelChanged
(DependencyObject d, DependencyPropertyChangedEventArgs e)
180
{
181
if
(d is FrameworkElement view && e.NewValue is
true
)
182
{
183
object
? viewModel = ViewModelLocator.Resolve(view.GetType());
184
if
(viewModel is not
null
)
185
{
186
view.DataContext = viewModel;
187
}
188
}
189
}
190
215
private
static
Type?
FindViewType
(
string
[] candidateTypeNames)
216
{
217
foreach
(
string
candidateTypeName
in
candidateTypeNames)
218
{
219
foreach
(var assembly
in
AppDomain.CurrentDomain.GetAssemblies())
220
{
221
Type? viewType = assembly.GetType(candidateTypeName);
222
if
(viewType is
null
)
223
{
224
continue
;
225
}
226
227
if
(!typeof(FrameworkElement).IsAssignableFrom(viewType))
228
{
229
continue
;
230
}
231
232
if
(viewType.IsAbstract)
233
{
234
continue
;
235
}
236
237
return
viewType;
238
}
239
}
240
241
return
null
;
242
}
243
}
244
}
Dreamine.MVVM.Locators
Definition
ContentControlNavigator.cs:10
Dreamine.MVVM.Locators.Wpf
Definition
ContentControlNavigator.cs:10
Dreamine.MVVM.Locators.Wpf.ViewModelBinder
Definition
ViewModelBinder.cs:16
Dreamine.MVVM.Locators.Wpf.ViewModelBinder.OnAutoWireViewModelChanged
static void OnAutoWireViewModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Definition
ViewModelBinder.cs:179
Dreamine.MVVM.Locators.Wpf.ViewModelBinder.ResolveView
static FrameworkElement ResolveView(object viewModel)
Definition
ViewModelBinder.cs:130
Dreamine.MVVM.Locators.Wpf.ViewModelBinder.GetAutoWireViewModel
static bool GetAutoWireViewModel(DependencyObject obj)
Definition
ViewModelBinder.cs:56
Dreamine.MVVM.Locators.Wpf.ViewModelBinder.FindViewType
static ? Type FindViewType(string[] candidateTypeNames)
Definition
ViewModelBinder.cs:215
Dreamine.MVVM.Locators.Wpf.ViewModelBinder.AutoWireViewModelProperty
static readonly DependencyProperty AutoWireViewModelProperty
Definition
ViewModelBinder.cs:25
Dreamine.MVVM.Locators.Wpf.ViewModelBinder.SetAutoWireViewModel
static void SetAutoWireViewModel(DependencyObject obj, bool value)
Definition
ViewModelBinder.cs:85
ViewModelBinder.cs
다음에 의해 생성됨 :
1.17.0