using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using Aitex.Core.RT.Log;
namespace Aitex.Core.UI.View.Frame
{
///
/// Interaction logic for CenterView.xaml
///
public partial class CenterView : UserControl
{
Dictionary _views = new Dictionary();
public CenterView()
{
InitializeComponent();
}
public void CreateView(List views, string assemblyName)
{
foreach (ViewItem item in views)
{
if (item.SubView == null || item.SubView.Count == 0)
continue;
CenterTabView tab = new CenterTabView();
tab.Tag = item;
foreach (ViewItem subItem in item.SubView)
{
Type t = Assembly.Load(assemblyName).GetType(subItem.ViewClass);
if (t == null)
t = Assembly.GetExecutingAssembly().GetType(subItem.ViewClass);
if (t == null)
throw new ApplicationException(string.Format("在程序集{0}中,没有找到{1},请检查UILayout配置文件中的设置", assemblyName, subItem.ViewClass));
UserControl uc;
try
{
uc = (UserControl)Activator.CreateInstance(t);
}
catch (Exception ex)
{
LOG.Write(ex);
throw new ApplicationException(string.Format("窗口{0}初始化失败,错误信息:{1}", subItem.ViewClass, ex.Message));
}
if (!string.IsNullOrEmpty(subItem.ViewModelClass))
{
Type vmType = Assembly.Load(assemblyName).GetType(subItem.ViewModelClass);
if (vmType == null)
vmType = Assembly.GetExecutingAssembly().GetType(subItem.ViewModelClass);
if (vmType == null)
throw new ApplicationException(string.Format("没有找到UILayout配置文件中的{0} in {1}", subItem.ViewModelClass, assemblyName));
uc.DataContext = Activator.CreateInstance(vmType);
}
tab.Add(subItem, uc);
}
_views[item.Id] = tab;
}
}
public void SetSelection(string id)
{
if (!_views.ContainsKey(id))
return;
gridContent.Children.Clear();
gridContent.Children.Add(_views[id]);
}
public string GetCurrentViewName(string culture)
{
if (gridContent.Children.Count == 0)
return string.Empty;
ViewItem info = (gridContent.Children[0] as CenterTabView).Tag as ViewItem;
if (!string.IsNullOrEmpty(culture) && info.GlobalName.ContainsKey(culture))
return info.GlobalName[culture];
return info.Name;
}
public UserControl GetView(string id)
{
foreach (var item in _views)
{
if (item.Value.FindView(id) != null)
return item.Value.FindView(id);
}
return null;
}
public TabItem GetTab(string id)
{
foreach (var item in _views)
{
if (item.Value.FindTab(id) != null)
return item.Value.FindTab(id);
}
return null;
}
public void SetCulture(string culture)
{
foreach (var item in _views)
{
item.Value.SetCulture(culture);
}
}
}
}