/// <summary> /// 获取并创建WPF的UI元素 /// </summary> private void SetUIElement() { UIElementName = GetType().Name; UIElementName = UIElementName.Replace("Vm_", ""); //去除命名规范的前缀 UIElementName = UIElementName.Replace("`1", ""); //去除泛型的特定标识 if (UIElementName.StartsWith("Window")) { UIElementName = UIElementName.TrimStart("Window".ToCharArray()); UIElement = Get <Window>(); (UIElement as Window).Closing += delegate(object sender, CancelEventArgs e) { OnElementClosing?.Invoke(sender, e); }; } else if (UIElementName.StartsWith("Page")) { UIElementName = UIElementName.TrimStart("Page".ToCharArray()); UIElement = Get <Page>(); } else if (UIElementName.StartsWith("UC")) { UIElementName = UIElementName.TrimStart("UC".ToCharArray()); UIElement = Get <UserControl>(); } else { throw new FrameworkException(103, string.Format("元素[{0}]不符合命名规范!", UIElementName)); } UIElement.Loaded += delegate(object sender, RoutedEventArgs e) { OnElementLoaded?.Invoke(sender, e); }; UIElement.Unloaded += delegate(object sender, RoutedEventArgs e) { OnElementUnLoaded?.Invoke(sender, e); }; }