the-operating-system-mark-up-extension-c-code

using System;
using System.Windows.Markup;

namespace i4.WPF.Custom.MarkupExtensions
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use these Markup Extensions in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:i4.WPF.Custom.MarkupExtensions"
    /// or
    ///     xmlns:MyNamespace="http://www.intelligence4.net/WPF/Custom/MarkupExtensionsssdk/2009/xaml"
    ///
    ///
    /// Step 1b) Using these Markup Extensions in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:i4.WPF.Custom.MarkupExtensions;assembly=i4MarkupExtensions"
    /// or
    ///     xmlns:MyNamespace="http://www.intelligence4.net/WPF/Custom/MarkupExtensionsssdk/2009/xaml"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your Markup Extensions in the XAML file.
    ///
    ///     MyNamespace:OperatingSystem
    ///
    /// XAML Markup Extension to bind dependency property (DateTime or String)    
    /// to the current date and time (updated each second)    
    /// </summary>
    [MarkupExtensionReturnType(typeof (string))]
    public class OperatingSystemExtension : MarkupExtension
    {
        /// <summary>        
        /// Overrides MarkupExtension.ProvideValue to return         
        /// the current datetime and hook a timer to update the         
        /// target propery each second         
        /// (using DispatcherTime with Background priority)        
        /// </summary>        
        /// <param name="serviceProvider"></param>        
        /// <returns></returns>        
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            string strSys = string.Empty;
            // Get OperatingSystem information from the system namespace.
            OperatingSystem osInfo = Environment.OSVersion;

            // Determine the platform.
            switch (osInfo.Platform)
            {
                    // Platform is Windows 95, Windows 98, 
                    // Windows 98 Second Edition, or Windows Me.
                case PlatformID.Win32Windows:

                    switch (osInfo.Version.Minor)
                    {
                        case 0:
                            strSys = "Windows 95";
                            break;
                        case 10:
                            strSys = osInfo.Version.Revision.ToString() == "2222A"
                                         ? "Windows 98 Second Edition"
                                         : "Windows 98";
                            break;
                        case 90:
                            strSys = "Windows Me";
                            break;
                    }
                    break;

                    // Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000,
                    // or Windows XP.
                case PlatformID.Win32NT:

                    switch (osInfo.Version.Major)
                    {
                        case 3:
                            strSys = "Windows NT 3.51";
                            break;
                        case 4:
                            strSys = "Windows NT 4.0";
                            break;
                        case 5:
                            strSys = osInfo.Version.Minor == 0 ? "Windows 2000" : "Windows XP";
                            break;
                        case 6:
                            strSys = osInfo.Version.Minor == 0 ? "Windows Vista" : "Windows 7 Beta";
                            break;
                        case 7:
                            strSys = "Windows 7";
                            break;
                        case 8:
                            strSys = "Windows 8";
                            break;
                    }
                    break;
                case PlatformID.MacOSX:
                    strSys = "MacOSX";
                    break;
                case PlatformID.Unix:
                    strSys = "Unix";
                    break;
                case PlatformID.WinCE:
                    strSys = "WinCE";
                    break;
                case PlatformID.Xbox:
                    strSys = "Xbox";
                    break;
                case PlatformID.Win32S:
                    strSys = "Win32S";
                    break;
            }

            return strSys;
        }
    }
}
Advertisements

One Response to “the-operating-system-mark-up-extension-c-code”

  1. […] simple mark-up extension that displays current date and time in your XAML. The code can be found here, and the XAML here. Below is an example of the Now mark-up extension in […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: