Stylet-Learning/ToDoStylet/BaseViewManager.cs

77 lines
2.5 KiB
C#
Raw Permalink Normal View History

2019-10-16 01:14:43 -07:00
/**************************************************************************
*
* =================================
* CLR版本 4.0.30319.42000
* ToDoStylet
* StudentViewManager.cs
* =================================
* LQZ
* 2019-8-14 14:54:05
*
* =================================
*
*
*
* =================================
*
***************************************************************************/
using Stylet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace ToDoStylet
{
/// <summary>
/// 自定义特性,用于跨项目绑定前后台
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
sealed class ViewModelAttribute : Attribute
{
readonly Type viewModel;
public ViewModelAttribute(Type viewModel)
{
this.viewModel = viewModel;
}
public Type ViewModel
{
get { return viewModel; }
}
}
/// <summary>
/// 绑定view与viewmodel
/// </summary>
public class BaseViewManager : ViewManager
{
// 用于存储viewmodel与view类型的字典Dictionary of ViewModel type -> View type
private readonly Dictionary<Type, Type> viewModelToViewMapping;
public BaseViewManager(ViewManagerConfig config)
: base(config)
{
var mappings = from type in this.ViewAssemblies.SelectMany(x => x.GetExportedTypes())
let attribute = type.GetCustomAttribute<ViewModelAttribute>()
where attribute != null && typeof(UIElement).IsAssignableFrom(type)
select new { View = type, ViewModel = attribute.ViewModel };
this.viewModelToViewMapping = mappings.ToDictionary(x => x.ViewModel, x => x.View);
}
//根据viewmodel定位view
protected override Type LocateViewForModel(Type modelType)
{
Type viewType;
if (!this.viewModelToViewMapping.TryGetValue(modelType, out viewType))
throw new Exception(String.Format("Could not find View for ViewModel {0}", modelType.Name));
return viewType;
}
}
}