ViewModelBinder.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #if XFORMS
  2. namespace Caliburn.Micro.Core.Xamarin.Forms
  3. #else
  4. namespace Caliburn.Micro
  5. #endif
  6. {
  7. using System;
  8. using System.Linq;
  9. using System.Collections.Generic;
  10. using System.Reflection;
  11. using System.Threading.Tasks;
  12. #if XFORMS
  13. using UIElement = global::Xamarin.Forms.Element;
  14. using FrameworkElement = global::Xamarin.Forms.VisualElement;
  15. using DependencyProperty = global::Xamarin.Forms.BindableProperty;
  16. using DependencyObject = global::Xamarin.Forms.BindableObject;
  17. #elif WinRT81
  18. using Windows.UI.Xaml;
  19. using Microsoft.Xaml.Interactivity;
  20. #else
  21. using System.Windows;
  22. using System.Windows.Interactivity;
  23. using Caliburn.Micro.Core;
  24. #endif
  25. #if WINDOWS_PHONE
  26. using Microsoft.Phone.Controls;
  27. #endif
  28. /// <summary>
  29. /// Binds a view to a view model.
  30. /// </summary>
  31. public static class ViewModelBinder {
  32. const string AsyncSuffix = "Async";
  33. static readonly ILog Log = LogManager.GetLog(typeof(ViewModelBinder));
  34. /// <summary>
  35. /// Gets or sets a value indicating whether to apply conventions by default.
  36. /// </summary>
  37. /// <value>
  38. /// <c>true</c> if conventions should be applied by default; otherwise, <c>false</c>.
  39. /// </value>
  40. public static bool ApplyConventionsByDefault = true;
  41. /// <summary>
  42. /// Indicates whether or not the conventions have already been applied to the view.
  43. /// </summary>
  44. public static readonly DependencyProperty ConventionsAppliedProperty =
  45. DependencyPropertyHelper.RegisterAttached(
  46. "ConventionsApplied",
  47. typeof(bool),
  48. typeof(ViewModelBinder),
  49. false
  50. );
  51. /// <summary>
  52. /// Determines whether a view should have conventions applied to it.
  53. /// </summary>
  54. /// <param name="view">The view to check.</param>
  55. /// <returns>Whether or not conventions should be applied to the view.</returns>
  56. public static bool ShouldApplyConventions(FrameworkElement view) {
  57. var overriden = View.GetApplyConventions(view);
  58. return overriden.GetValueOrDefault(ApplyConventionsByDefault);
  59. }
  60. /// <summary>
  61. /// Creates data bindings on the view's controls based on the provided properties.
  62. /// </summary>
  63. /// <remarks>Parameters include named Elements to search through and the type of view model to determine conventions for. Returns unmatched elements.</remarks>
  64. public static Func<IEnumerable<FrameworkElement>, Type, IEnumerable<FrameworkElement>> BindProperties = (namedElements, viewModelType) => {
  65. var unmatchedElements = new List<FrameworkElement>();
  66. #if !XFORMS
  67. foreach (var element in namedElements) {
  68. var cleanName = element.Name.Trim('_');
  69. var parts = cleanName.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
  70. var property = viewModelType.GetPropertyCaseInsensitive(parts[0]);
  71. var interpretedViewModelType = viewModelType;
  72. for (int i = 1; i < parts.Length && property != null; i++) {
  73. interpretedViewModelType = property.PropertyType;
  74. property = interpretedViewModelType.GetPropertyCaseInsensitive(parts[i]);
  75. }
  76. if (property == null) {
  77. unmatchedElements.Add(element);
  78. Log.Info("Binding Convention Not Applied: Element {0} did not match a property.", element.Name);
  79. continue;
  80. }
  81. var convention = ConventionManager.GetElementConvention(element.GetType());
  82. if (convention == null) {
  83. unmatchedElements.Add(element);
  84. Log.Warn("Binding Convention Not Applied: No conventions configured for {0}.", element.GetType());
  85. continue;
  86. }
  87. var applied = convention.ApplyBinding(
  88. interpretedViewModelType,
  89. cleanName.Replace('_', '.'),
  90. property,
  91. element,
  92. convention
  93. );
  94. if (applied) {
  95. Log.Info("Binding Convention Applied: Element {0}.", element.Name);
  96. }
  97. else {
  98. Log.Info("Binding Convention Not Applied: Element {0} has existing binding.", element.Name);
  99. unmatchedElements.Add(element);
  100. }
  101. }
  102. #endif
  103. return unmatchedElements;
  104. };
  105. /// <summary>
  106. /// Attaches instances of <see cref="ActionMessage"/> to the view's controls based on the provided methods.
  107. /// </summary>
  108. /// <remarks>Parameters include the named elements to search through and the type of view model to determine conventions for. Returns unmatched elements.</remarks>
  109. public static Func<IEnumerable<FrameworkElement>, Type, IEnumerable<FrameworkElement>> BindActions = (namedElements, viewModelType) => {
  110. var unmatchedElements = namedElements.ToList();
  111. #if !XFORMS
  112. #if WinRT || XFORMS
  113. var methods = viewModelType.GetRuntimeMethods();
  114. #else
  115. var methods = viewModelType.GetMethods();
  116. #endif
  117. foreach (var method in methods) {
  118. var foundControl = unmatchedElements.FindName(method.Name);
  119. if (foundControl == null && IsAsyncMethod(method)) {
  120. var methodNameWithoutAsyncSuffix = method.Name.Substring(0, method.Name.Length - AsyncSuffix.Length);
  121. foundControl = unmatchedElements.FindName(methodNameWithoutAsyncSuffix);
  122. }
  123. if(foundControl == null) {
  124. Log.Info("Action Convention Not Applied: No actionable element for {0}.", method.Name);
  125. continue;
  126. }
  127. unmatchedElements.Remove(foundControl);
  128. #if WinRT81
  129. var triggers = Interaction.GetBehaviors(foundControl);
  130. if (triggers != null && triggers.Count > 0)
  131. {
  132. Log.Info("Action Convention Not Applied: Interaction.Triggers already set on {0}.", foundControl.Name);
  133. continue;
  134. }
  135. #endif
  136. var message = method.Name;
  137. var parameters = method.GetParameters();
  138. if (parameters.Length > 0) {
  139. message += "(";
  140. foreach (var parameter in parameters) {
  141. var paramName = parameter.Name;
  142. var specialValue = "$" + paramName.ToLower();
  143. if (MessageBinder.SpecialValues.ContainsKey(specialValue))
  144. paramName = specialValue;
  145. message += paramName + ",";
  146. }
  147. message = message.Remove(message.Length - 1, 1);
  148. message += ")";
  149. }
  150. Log.Info("Action Convention Applied: Action {0} on element {1}.", method.Name, message);
  151. Message.SetAttach(foundControl, message);
  152. }
  153. #endif
  154. return unmatchedElements;
  155. };
  156. static bool IsAsyncMethod(MethodInfo method) {
  157. return typeof(Task).IsAssignableFrom(method.ReturnType) &&
  158. method.Name.EndsWith(AsyncSuffix, StringComparison.OrdinalIgnoreCase);
  159. }
  160. /// <summary>
  161. /// Allows the developer to add custom handling of named elements which were not matched by any default conventions.
  162. /// </summary>
  163. public static Action<IEnumerable<FrameworkElement>, Type> HandleUnmatchedElements = (elements, viewModelType) => { };
  164. /// <summary>
  165. /// Binds the specified viewModel to the view.
  166. /// </summary>
  167. ///<remarks>Passes the the view model, view and creation context (or null for default) to use in applying binding.</remarks>
  168. public static Action<object, DependencyObject, object> Bind = (viewModel, view, context) => {
  169. #if !WinRT && !XFORMS
  170. // when using d:DesignInstance, Blend tries to assign the DesignInstanceExtension class as the DataContext,
  171. // so here we get the actual ViewModel which is in the Instance property of DesignInstanceExtension
  172. if (View.InDesignMode) {
  173. var vmType = viewModel.GetType();
  174. if (vmType.FullName == "Microsoft.Expression.DesignModel.InstanceBuilders.DesignInstanceExtension") {
  175. var propInfo = vmType.GetProperty("Instance", BindingFlags.Instance | BindingFlags.NonPublic);
  176. viewModel = propInfo.GetValue(viewModel, null);
  177. }
  178. }
  179. #endif
  180. Log.Info("Binding {0} and {1}.", view, viewModel);
  181. #if XFORMS
  182. var noContext = Caliburn.Micro.Xamarin.Forms.Bind.NoContextProperty;
  183. #else
  184. var noContext = Caliburn.Micro.Bind.NoContextProperty;
  185. #endif
  186. if ((bool)view.GetValue(noContext)) {
  187. Action.SetTargetWithoutContext(view, viewModel);
  188. }
  189. else {
  190. Action.SetTarget(view, viewModel);
  191. }
  192. var viewAware = viewModel as IViewAware;
  193. if (viewAware != null) {
  194. Log.Info("Attaching {0} to {1}.", view, viewAware);
  195. viewAware.AttachView(view, context);
  196. }
  197. if ((bool)view.GetValue(ConventionsAppliedProperty)) {
  198. return;
  199. }
  200. var element = View.GetFirstNonGeneratedView(view) as FrameworkElement;
  201. if (element == null) {
  202. return;
  203. }
  204. #if WINDOWS_PHONE
  205. BindAppBar(view);
  206. #endif
  207. if (!ShouldApplyConventions(element)) {
  208. Log.Info("Skipping conventions for {0} and {1}.", element, viewModel);
  209. #if WINDOWS_PHONE
  210. view.SetValue(ConventionsAppliedProperty, true); // we always apply the AppBar conventions
  211. #endif
  212. return;
  213. }
  214. var viewModelType = viewModel.GetType();
  215. #if SL5 || NET45
  216. var viewModelTypeProvider = viewModel as ICustomTypeProvider;
  217. if (viewModelTypeProvider != null) {
  218. viewModelType = viewModelTypeProvider.GetCustomType();
  219. }
  220. #endif
  221. #if XFORMS
  222. IEnumerable<FrameworkElement> namedElements = new List<FrameworkElement>();
  223. #else
  224. var namedElements = BindingScope.GetNamedElements(element);
  225. #endif
  226. #if SILVERLIGHT
  227. namedElements.Apply(x => x.SetValue(
  228. View.IsLoadedProperty,
  229. element.GetValue(View.IsLoadedProperty))
  230. );
  231. #endif
  232. namedElements = BindActions(namedElements, viewModelType);
  233. namedElements = BindProperties(namedElements, viewModelType);
  234. HandleUnmatchedElements(namedElements, viewModelType);
  235. view.SetValue(ConventionsAppliedProperty, true);
  236. };
  237. #if WINDOWS_PHONE
  238. static void BindAppBar(DependencyObject view) {
  239. var page = view as PhoneApplicationPage;
  240. if (page == null || page.ApplicationBar == null) {
  241. return;
  242. }
  243. var triggers = Interaction.GetTriggers(view);
  244. foreach(var item in page.ApplicationBar.Buttons) {
  245. var button = item as IAppBarActionMessage;
  246. if (button == null || string.IsNullOrEmpty(button.Message)) {
  247. continue;
  248. }
  249. var parsedTrigger = Parser.Parse(view, button.Message).First();
  250. var trigger = new AppBarItemTrigger(button);
  251. var actionMessages = parsedTrigger.Actions.OfType<ActionMessage>().ToList();
  252. actionMessages.Apply(x => {
  253. x.applicationBarSource = button;
  254. parsedTrigger.Actions.Remove(x);
  255. trigger.Actions.Add(x);
  256. });
  257. triggers.Add(trigger);
  258. }
  259. foreach (var item in page.ApplicationBar.MenuItems) {
  260. var menuItem = item as IAppBarActionMessage;
  261. if (menuItem == null || string.IsNullOrEmpty(menuItem.Message)) {
  262. continue;
  263. }
  264. var parsedTrigger = Parser.Parse(view, menuItem.Message).First();
  265. var trigger = new AppBarItemTrigger(menuItem);
  266. var actionMessages = parsedTrigger.Actions.OfType<ActionMessage>().ToList();
  267. actionMessages.Apply(x => {
  268. x.applicationBarSource = menuItem;
  269. parsedTrigger.Actions.Remove(x);
  270. trigger.Actions.Add(x);
  271. });
  272. triggers.Add(trigger);
  273. }
  274. }
  275. #endif
  276. }
  277. }