View.cs :  » Windows-Presentation-Foundation » Caliburn » Caliburn » PresentationFramework » Views » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Windows Presentation Foundation » Caliburn 
Caliburn » Caliburn » PresentationFramework » Views » View.cs
namespace Caliburn.PresentationFramework.Views{
    using System.Linq;
    using System.Windows;
    using System.Windows.Markup;
    using Core;
    using ViewModels;

    /// <summary>
    /// Hosts attached properties related to view models.
    /// </summary>
    public static class View
    {
        private static IViewLocator _viewLocator;
        private static IViewModelBinder _viewModelBinder;

        /// <summary>
        /// Initializes the framework with the specified view locator and view model binder.
        /// </summary>
        /// <param name="viewLocator">The view locator.</param>
        /// <param name="viewModelBinder">The view model binder.</param>
        public static void Initialize(IViewLocator viewLocator, IViewModelBinder viewModelBinder)
        {
            _viewLocator = viewLocator;
            _viewModelBinder = viewModelBinder;
        }

        /// <summary>
        /// A dependency property for assigning a context to a particular portion of the UI.
        /// </summary>
        public static readonly DependencyProperty ContextProperty =
            DependencyProperty.RegisterAttached(
                "Context",
                typeof(object),
                typeof(View),
                new PropertyMetadata(OnContextChanged)
                );

        /// <summary>
        /// Gets the context.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <returns></returns>
        public static object GetContext(DependencyObject d)
        {
            return d.GetValue(ContextProperty);
        }

        /// <summary>
        /// Sets the context.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <param name="value">The value.</param>
        public static void SetContext(DependencyObject d, object value)
        {
            d.SetValue(ContextProperty, value);
        }

        /// <summary>
        /// A dependency property for attaching a model to the UI.
        /// </summary>
        public static readonly DependencyProperty ModelProperty =
            DependencyProperty.RegisterAttached(
                "Model",
                typeof(object),
                typeof(View),
                new PropertyMetadata(OnModelChanged)
                );

        /// <summary>
        /// Gets the model.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <returns></returns>
        public static object GetModel(DependencyObject d)
        {
            return d.GetValue(ModelProperty);
        }

        /// <summary>
        /// Sets the model.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <param name="value">The value.</param>
        public static void SetModel(DependencyObject d, object value)
        {
            d.SetValue(ModelProperty, value);
        }


        /// <summary>
        /// A dependency property for assigning an <see cref="IViewLocator"/> to a UI element.
        /// </summary>
        public static readonly DependencyProperty StrategyProperty =
            DependencyProperty.RegisterAttached(
                "Strategy",
                typeof(IViewLocator),
                typeof(View),
                new PropertyMetadata(OnStrategyChanged)
                );

        /// <summary>
        /// Gets the <see cref="IViewLocator"/>.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <returns>The strategy.</returns>
        public static IViewLocator GetStrategy(DependencyObject d)
        {
            return d.GetValue(StrategyProperty) as IViewLocator;
        }

        /// <summary>
        /// Sets the <see cref="IViewLocator"/>.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <param name="value">The value.</param>
        public static void SetStrategy(DependencyObject d, IViewLocator value)
        {
            d.SetValue(StrategyProperty, value);
        }

        /// <summary>
        /// A dependency property which allows the override of convention application behavior.
        /// </summary>
        public static readonly DependencyProperty ApplyConventionsProperty =
            DependencyProperty.RegisterAttached(
                "ApplyConventions",
                typeof(bool?),
                typeof(View),
                null
                );

        /// <summary>
        /// Gets the convention application behavior.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <returns>The strategy.</returns>
        public static bool? GetApplyConventions(DependencyObject d)
        {
            return (bool?)d.GetValue(ApplyConventionsProperty);
        }

        /// <summary>
        /// Sets the convention application behavior.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <param name="value">The value.</param>
        public static void SetApplyConventions(DependencyObject d, bool? value)
        {
            d.SetValue(ApplyConventionsProperty, value);
        }

        private static void OnModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var locator = GetStrategy(d) ?? _viewLocator;

            if (locator == null)
                return;

            if(e.OldValue == e.NewValue)
                return;

            if(e.NewValue != null)
            {
                var context = GetContext(d);
                var view = locator.Locate(e.NewValue, d, context);

                _viewModelBinder.Bind(e.NewValue, view, context);

                SetContentProperty(d, view);
            }
            else SetContentProperty(d, e.NewValue);
        }

        private static void OnContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var locator = GetStrategy(d) ?? _viewLocator;

            if (locator == null)
                return;

            if (e.OldValue == e.NewValue)
                return;

            var model = GetModel(d);
            if(model == null)
                return;

            var view = locator.Locate(model, d, e.NewValue);

            _viewModelBinder.Bind(model, view, e.NewValue);

            SetContentProperty(d, view);
        }

        private static void OnStrategyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == e.OldValue)
                return;

            var locator = e.NewValue as IViewLocator ?? _viewLocator;

            if (locator == null)
                return;

            var model = GetModel(d);
            if (model == null)
                return;

            var context = GetContext(d);
            var view = locator.Locate(model, d, context);

            _viewModelBinder.Bind(model, view, context);
            SetContentProperty(d, view);
        }

        private static void SetContentProperty(DependencyObject d, object view)
        {
            var type = d.GetType();
            var contentProperty = type.GetAttributes<ContentPropertyAttribute>(true)
                .FirstOrDefault() ?? new ContentPropertyAttribute("Content");

            type.GetProperty(contentProperty.Name)
                .SetValue(d, view, null);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.