void FillCandidateNameRanks(WiringCandidatePair candidateVmType) { var efffectiveAliasSplit = BindHelper.SplitNameByCase(candidateVmType.WiringName); var desiredXClassNameSplit = BindHelper.SplitNameByCase(candidateVmType.DesiredXCLassName); var desiredXNameSplit = BindHelper.SplitNameByCase(candidateVmType.DesiredXName); candidateVmType.WiringNameRank = efffectiveAliasSplit.Count; candidateVmType.DesiredXClassNameRank = desiredXClassNameSplit.Count; candidateVmType.DesiredXNameRank = desiredXNameSplit.Count; candidateVmType.DesiredXClassNameMatchToWiringNameRank = BindHelper.CalculateMatchingRank(desiredXClassNameSplit, efffectiveAliasSplit); candidateVmType.DesiredXNameMatchToWiringNameRank = BindHelper.CalculateMatchingRank(desiredXNameSplit, efffectiveAliasSplit); }
object ProcessAutoWiringToViewModel(FrameworkElement frameworkElement) { List <XClassTypeNameElement> parentXClassList = BindHelper.FindNonSystemParentClassNames(frameworkElement, UseTheFirstOne); //collect in the here below all possible candidates var viewModelCandidates = new List <WiringCandidatePair>(); string desiredVmNameSpace = string.Empty; foreach (XClassTypeNameElement parentXClass in parentXClassList) { // Split a full type name in the sections var parentFullClassNameSections = new List <string>(parentXClass.FullxClassTypeName.Split('.')); if ((!string.IsNullOrEmpty(ViewsNamespaceSuffixSection)) && (!string.IsNullOrEmpty(ViewModelsNamespaceSuffixSection))) { if (parentFullClassNameSections.Count >= 3) { { // if a name space suffix section contains a "Views"(default see prop. ViewsNameSpaceSuffixSection), // this section will be replaced on "ViewModels"(default see prop.ViewModelsNameSpaceSuffixSection) if (parentFullClassNameSections[parentFullClassNameSections.Count - 2].Contains(ViewsNamespaceSuffixSection)) { parentFullClassNameSections[parentFullClassNameSections.Count - 2] = ViewModelsNamespaceSuffixSection; } } } else { // If there is no a suffix section, i.e a name space has only one or two, the suffix section "ViewModels"(default see prop.ViewModelsNameSpaceSuffixSection) // will be added. parentFullClassNameSections.Insert(parentFullClassNameSections.Count - 1, ViewModelsNamespaceSuffixSection); } } // Form a desired View Model namespace for a type. desiredVmNameSpace = string.Join(".", parentFullClassNameSections.ToArray(), 0, parentFullClassNameSections.Count - 1); if (!string.IsNullOrEmpty(ViewModelNamespaceOverwrite)) { desiredVmNameSpace = ViewModelNamespaceOverwrite; } string desiredVmXName = string.Empty; string desiredVmXClassName = string.Empty; if (!string.IsNullOrEmpty(ViewModelNameOverwrite)) { // Overwrite the names i.e. x:Class x:Name desiredVmXClassName = ViewModelNameOverwrite; } else { if ((!string.IsNullOrEmpty(OldViewNamePart)) && (!string.IsNullOrEmpty(NewViewModelNamePart))) { // Form a desired View Model name (i.e. class name) for a type. // If a type name (i.e. class name) contains "View"(default see prop."OldNamePart"), it will be replaced on "ViewModel"(default see prop."NewNamePart"). desiredVmXClassName = parentFullClassNameSections[parentFullClassNameSections.Count - 1]; if (!desiredVmXClassName.Contains(NewViewModelNamePart)) { desiredVmXClassName = desiredVmXClassName.Replace(OldViewNamePart, NewViewModelNamePart); } if (!string.IsNullOrEmpty(parentXClass.XNameForXClass)) { desiredVmXName = parentXClass.XNameForXClass; if (!desiredVmXName.Contains(NewViewModelNamePart)) { desiredVmXName = desiredVmXName.Replace(OldViewNamePart, NewViewModelNamePart); } } } } #if DEBUG if (!string.IsNullOrEmpty(desiredVmXClassName)) { Debug.WriteLine("#INFO#-A-W-V-M# Desired x:Class, Type: {0}.{1}", desiredVmNameSpace, desiredVmXClassName); } if (!string.IsNullOrEmpty(desiredVmXName)) { Debug.WriteLine("#INFO#-A-W-V-M# Desired x:Name, Type: {0}.{1} ", desiredVmNameSpace, desiredVmXName); } #endif var candidateTypes = BindHelper.ResolveTypesByNameSpace(desiredVmNameSpace); foreach (Type item in candidateTypes) { WiringCandidatePair candidateVmType = new WiringCandidatePair(); candidateVmType.XClass = parentXClass; candidateVmType.WiringType = item; candidateVmType.WiringName = item.Name; candidateVmType.DesiredXCLassName = desiredVmXClassName; candidateVmType.DesiredXName = desiredVmXName; viewModelCandidates.Add(candidateVmType); FillCandidateNameRanks(candidateVmType); // Process attributes for a class #if !WINDOWS_UWP var attribV = Attribute.GetCustomAttributes(item, typeof(ViewModelClassAliasAttribute)); #else var attribV = item.GetTypeInfo().GetCustomAttributes(typeof(ViewModelClassAliasAttribute)); #endif if (attribV != null) { foreach (Attribute att in attribV) { var list = ((ViewModelClassAliasAttribute)att).Aliases; foreach (var alias in list) { candidateVmType = new WiringCandidatePair(); candidateVmType.XClass = parentXClass; candidateVmType.WiringType = item; candidateVmType.WiringName = alias; candidateVmType.DesiredXCLassName = desiredVmXClassName; candidateVmType.DesiredXName = desiredVmXName; viewModelCandidates.Add(candidateVmType); FillCandidateNameRanks(candidateVmType); } } } } } WiringCandidatePair locatedItem = null; if (viewModelCandidates.Count == 0) { Debug.WriteLine("#ERROR#-A-W-V-M# Cannot locate candidate Types in namespace: " + desiredVmNameSpace); return(null); } List <WiringCandidatePair> wiringTemp; if (locatedItem == null) { wiringTemp = viewModelCandidates.Where(itm => itm.IsXNameMatch).ToList(); if (wiringTemp.Count > 0) { locatedItem = wiringTemp[0]; } } if (locatedItem == null) { wiringTemp = viewModelCandidates.Where(itm => itm.IsXClassNameMatch).ToList(); if (wiringTemp.Count > 0) { locatedItem = wiringTemp[0]; } } if ((locatedItem == null) && (UseMaxNameSubMatch)) { //if (locatedItem == null) { wiringTemp = viewModelCandidates.Where(itm => itm.IsXNameSubMatch).ToList(); if (wiringTemp.Count > 0) { locatedItem = wiringTemp[0]; } } if (locatedItem == null) { wiringTemp = viewModelCandidates.Where(itm => itm.IsXClassNameSubMatch).ToList(); if (wiringTemp.Count > 0) { locatedItem = wiringTemp[0]; } } } if (locatedItem == null) { Debug.WriteLine("#ERROR#-A-W-V-M# Cannot locate proper candidate Types in: " + desiredVmNameSpace); return(null); } #if DEBUG else { Debug.WriteLine("#INFO#-A-W-V-M# Located Type: " + locatedItem.WiringFullName); } #endif object resolvedObject = null; // Resolve over the IOC container - the first. if (ResolveIocContainer && BindHelper.IsIocContainerActive) { if (ServiceProvider != null) { try { resolvedObject = ServiceProvider.GetService(locatedItem.WiringType); } catch { } #if DEBUG if (resolvedObject != null) { Debug.WriteLine("#INFO#-A-W-V-M# Resolved with IoC, GetInstance(typeof({0}))", new object[] { locatedItem.WiringFullName }); } #endif } else { try { resolvedObject = Activator.CreateInstance(locatedItem.WiringType); } catch { } #if DEBUG if (resolvedObject != null) { Debug.WriteLine("#INFO#-A-W-V-M# Resolved with Activator Type: " + locatedItem.WiringFullName); } #endif } } // Resolve over the resources container the second. if ((ResolveResources) && (resolvedObject == null)) { if (!string.IsNullOrEmpty(locatedItem.DesiredXName)) { resolvedObject = BindHelper.LocateResource(frameworkElement, locatedItem.DesiredXName); #if DEBUG if (resolvedObject != null) { Debug.WriteLine("#INFO#-A-W-V-M# Resolved with WPF Resource Key: " + locatedItem.DesiredXName); } #endif } resolvedObject = BindHelper.LocateResource(frameworkElement, locatedItem.WiringTypeName); #if DEBUG if (resolvedObject != null) { Debug.WriteLine("#INFO#-A-W-V-M# Resolved with WPF Resource Key: " + locatedItem.WiringTypeName); } #endif if (resolvedObject == null) { resolvedObject = BindHelper.LocateResource(frameworkElement, locatedItem.WiringFullName); #if DEBUG if (resolvedObject != null) { Debug.WriteLine("#INFO#-A-W-V-M# Resolved with WPF Resource Key: " + locatedItem.WiringFullName); } #endif } if (resolvedObject == null) { resolvedObject = BindHelper.LocateResource(frameworkElement, locatedItem.WiringType); #if DEBUG if (resolvedObject != null) { Debug.WriteLine("#INFO#-A-W-V-M# Resolved with WPF Resource Key type of : " + locatedItem.WiringFullName); } #endif } } // Create instance over the real type the third. if ((ResolveCreateInstance) && (resolvedObject == null)) { try { resolvedObject = Activator.CreateInstance(locatedItem.WiringType); } catch { // ignored } #if DEBUG if (resolvedObject != null) { Debug.WriteLine("#INFO#-A-W-V-M# Resolved with Activator Type: " + locatedItem.WiringFullName); } #endif } if (resolvedObject != null) { // setup resolved object to property DependencyProperty target = BindHelper.LocateDependencyProperty(TargetPropertyName, frameworkElement); if (target != null) { frameworkElement.SetValue(target, resolvedObject); // store auto resolved ViewModel into BindXAML.SetAutoWiredViewModel(frameworkElement, resolvedObject); } } #if DEBUG if (resolvedObject == null) { Debug.WriteLine("#ERROR#-A-W-V-M# Cannot Resolve Type: " + locatedItem.WiringFullName); } #endif return(resolvedObject); }