hdolder.com srl  

Software R&D
 

   hdc Home    |    Content    |    Blog    |    Director    |    Contact    |    mail us   

 


M&P BXaml in .NET, Part 2

Posted on July 30, 2013 by Herman Dolder
Updated on may 27, 2014
In Part 1 we have shown an example of a .NET WinForm application instanciated using BXaml.NET (the .NET version of BXaml). The example application was a "Color Mixer" and we presented two variants of code:
  • With code-behind, using an events handler.
  • * No code-behind, using an M&P BLOCK.

Instantiation of a WPF application

In this article we will look at the WPF version of the same "Color Mixer" application:

As the WPF version is "isomorphic" with the WinForm version we'll show only the code of the "No code-behind, using an M&P BLOCK" variant.

The "BSolidColorBrush" BLOCK here is analogous to the "BColorMixer" BLOCK of the WinForms example

using System.Drawing;
using blkx;
using System;
using System.Windows.Markup;
using System.Windows.Media;
using obx;
using dlr;

namespace mpx
{
    public class BSolidColorBrush : BLOCKBase
    {
        public BSolidColorBrush()
            : base()
        {
            _SCBrush = new SolidColorBrush(Colors.Gray);  // <<<<<<<<<<< 
        }

        private double _InRed = 128;  // <<<
        public double InRed
        {
            set
            {
                if (_InRed == value)
                    return;

                _InRed = value;

                MixColors();
            }
        }

        private double _InGreen = 128;  // <<<< 
        public double InGreen
        {
            set
            {
                if (_InGreen == value)
                    return;

                _InGreen = value;

                MixColors();
            }
        }

        private double _InBlue = 128;  // <<
        public double InBlue
        {
            set
            {
                if (_InBlue == value)
                    return;

                _InBlue = value;

                MixColors();
            }
        }

        private SolidColorBrush _SCBrush;
        public SolidColorBrush SCBrush
        {
            get
            {
                return _SCBrush;
            }
            set
            {
                _SCBrush = value;
                OnPropertyChanged("SCBrush");
            }
        }

        void MixColors()
        {
                OB.EnqueueUIT(() =>
                  {
                      SolidColorBrush mySolidColorBrush = new SolidColorBrush();

                      // Describes the brush's color using RGB values. 
                      // Each value has a range of 0-255.
                      mySolidColorBrush.Color = System.Windows.Media.Color.FromArgb(255, Convert.ToByte(Math.Round(_InRed)), Convert.ToByte(Math.Round(_InGreen)), Convert.ToByte(Math.Round(_InBlue)));

                      SCBrush = mySolidColorBrush;
                  });
        }

    }  // class
} // namespace
      

and the code for the WPF window is

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using blkx;
using obx;

namespace mpx
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            OB.Setup("Demo");
            DemoApp x = new DemoApp();
            x.BStart();
        }
    }

    public class DemoApp : BLOCKBase
    {
        public DemoApp()
            : base()
        {
            CreateInnerBlocks();
        }

        void CreateInnerBlocks()
        {
                form = (Window)OB.XA.LoadAndStart(this, XamlCode);
                form.ShowDialog();
        }

        public void BStart()
        {
        }

        Window form;

        private string XamlCode =      
"<mpXaml  " +
"  xmlns:sw='System.Windows'  " +
"  xmlns:wpf='System.Windows.Controls'    " +
"  xmlns:wpfsh='System.Windows.Shapes'    " +
"  xmlns:mpx='mpx'    " +
"  xmlns:blkx='blkx'    " +
"  >  " +
" <blkx:BBoard> " +
" <mpx:BGreenTheme /> " +
" <sw:Window id='AppMainWindow' " +
"     Title='WPF Color Mixer' " +
"     Height='230' " +
"     Width='400' > " +
"    <blkx:BStyle id='slider1' " +
"                Style='{Orientation:`Vertical`, Height:`130`, Width:`45` }' " +
"        />   " +
"    <blkx:BStyle id='slider2' " +
"                Style='{basedOn:`slider1`, Minimum:`0`, Maximum:`255`, Value:`128`, SmallChange:`1` }'   " +
"        />   " +
"    <blkx:BStyle id='txtb1'   " +
"                Style='{FontSize:`14`, Width:`40`, Height:`20` }'    " +
"        />   " +
"    <wpf:Canvas  " +
"      id='LayoutRoot'    " +
"      Width='400'    " +
"      Height='230'   " +
"      HorizontalAlignment='Left' " +
"      Background='#006400'   " +
"      VerticalAlignment='Top'>   " +
"      <wpf:Slider    " +
"        id='RedScroll'   " +
"        BStyle='slider2' " +
"        Canvas.Left='10' " +
"        Canvas.Top='30' />   " +
"      <wpf:Slider    " +
"        id='GreenScroll' " +
"        BStyle='slider2' " +
"        Canvas.Left='50' " +
"        Canvas.Top='30' />   " +
"      <wpf:Slider    " +
"        id='BlueScroll'  " +
"        BStyle='slider2' " +
"        Canvas.Left='100'    " +
"        Canvas.Top='30' />   " +
"      <wpf:TextBlock " +
"        BStyle='txtb1'   " +
"        Foreground='Orange'  " +
"        Canvas.Left='10' " +
"        Canvas.Top='5'   " +
"        Text='Red' />    " +
"      <wpf:TextBlock " +
"        BStyle='txtb1'   " +
"        Foreground='LightGreen'  " +
"        Canvas.Left='55' " +
"        Canvas.Top='5'   " +
"        Text='Green' />  " +
"      <wpf:TextBlock " +
"        BStyle='txtb1'   " +
"        Foreground='LightBlue'   " +
"        Canvas.Left='105'    " +
"        Canvas.Top='5'   " +
"        Text='Blue' />   " +
"      <wpf:TextBlock " +
"        BStyle='txtb1'   " +
"        Foreground='Orange'  " +
"        Canvas.Left='15' " +
"        Canvas.Top='160' " +
"        id='RedValue'    " +
"        Text='{cls:`Binding`, SourcePath:`RedScroll.Value`,  Converter:`BToStringValConv` }' />  " +
"      <wpf:TextBlock " +
"        BStyle='txtb1'   " +
"        Foreground='LightGreen'  " +
"        Canvas.Left='60' " +
"        Canvas.Top='160' " +
"        id='GreenValue'  " +
"        Text='{cls:`Binding`, SourcePath:`GreenScroll.Value`,  Converter:`BToStringValConv` }' />    " +
"      <wpf:TextBlock " +
"        BStyle='txtb1'   " +
"        Foreground='LightBlue'   " +
"        Canvas.Left='105'    " +
"        Canvas.Top='160' " +
"        id='BlueValue'   " +
"        Text='{cls:`Binding`, SourcePath:`BlueScroll.Value`,  Converter:`BToStringValConv` }' /> " +
"      <mpx:BSolidColorBrush  " +
"        id='ColorMixer1' " +
"        InBlue='{cls:`Binding`, SourcePath:`BlueScroll.Value`}'  " +
"        InRed='{cls:`Binding`, SourcePath:`RedScroll.Value`}'    " +
"        InGreen='{cls:`Binding`, SourcePath:`GreenScroll.Value`}'    " +
"            />   " +
"      <wpfsh:Rectangle   " +
"        id='ColorPanel'  " +
"        Fill='{cls:`Binding`, SourcePath:`ColorMixer1.SCBrush` }'    " +
"        Canvas.Left='180'    " +
"        Width='200'  " +
"        Height='190' />  " +
"    </wpf:Canvas>    " +
"  </sw:Window>   " +
" </blkx:BBoard> " +
"</mpXaml>    ";

    } // class
} // namespace      

The code of the " BGreenTheme" BLOCK is


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using blkx;
using obx;

namespace mpx
{
    public class BGreenTheme : BLOCKBase
    {
        public BGreenTheme()
            : base()
        {
            CreateInnerBlocks();
        }

        void CreateInnerBlocks()
        {
            object xx = OB.LoadAndStart(this, XamlCode);
        }

        public void BStart()
        {
        }

        private string XamlCode =
"<mpXaml  " +
"  xmlns:mpx='mpx'    " +
"  xmlns:blkx='blkx'  " +
"  >  " +
"<blkx:BBoard>   " +
"  <blkx:BStyle id='win1' " +
"      TargetType='System.Windows.Window' " +
"        Style='{WindowStartupLocation:`CenterScreen`, Background:`#006400`, Foreground:`#FFFFFF`, BorderBrush:`#FF000019`, ShowInTaskbar:`True`,ResizeMode:`CanMinimize` }' />   " +
"<blkx:BStyle id='common1'    " +
"  Style='{Background:`#FF000000`, FontFamily:`Microsoft Sans Serif`, FontSize:`14`, FontWeight:`Bold`, Foreground:`#FFFFFFFF` }' />  " +
        "<blkx:BStyle id='tblk1'  " +
"  TargetType='System.Windows.Controls.TextBlock' " +
"  Style='{basedOn:`common1`, Background:`Transparent`, Width:`40`, Height:`23`, TextAlign:`BottomLeft`}' />   " +
"<blkx:BStyle id='label1' " +
"  TargetType='System.Windows.Controls.Label' " +
"  Style='{basedOn:`tblk1`}' />   " +
"<blkx:BStyle id='btn1'   " +
"  TargetType='System.Windows.Forms.Button'   " +
"  Style='{basedOn:`common1`,Margin:`4, 5, 4, 5`, Width:`80`, Height:`23`, TextAlign:`MiddleCenter`}' />  " +
"<blkx:BStyle id='tbx1'   " +
"  TargetType='System.Windows.Forms.TextBox'  " +
"  Style='{basedOn:`common1`, Width:`80`, Height:`23`, AcceptsReturn:`True`, BorderStyle:`FixedSingle`, Margin:`4, 5, 4, 5`, TextAlign:`BottomLeft`}' />  " +
"<blkx:BStyle id='tbxml1' " +
"  Description='TextBox Multiline'    " +
"  TargetType='System.Windows.Forms.TextBox:ML'   " +
"  Style='{basedOn:`common1`, Width:`100`, Height:`50`, AcceptsReturn:`True`, " +
"MaxLength:`0`, Multiline:`True`, ScrollBars:`Vertical`, Margin:`4, 5, 4, 5`}' /> " +
"<blkx:BStyle id='gbx1'   " +
"  TargetType='System.Windows.Forms.GroupBox' " +
"  Style='{basedOn:`common1` }' />    " +
"<blkx:BStyle id='cbx1'   " +
"  TargetType='System.Windows.Forms.CheckBox' " +
"  Style='{basedOn:`common1` }' />    " +
"<blkx:BStyle id='combbx1'    " +
"  TargetType='System.Windows.Forms.ComboBox' " +
"  Style='{basedOn:`common1` }' />    " +
"<blkx:BStyle id='radiob1'    " +
"  TargetType='System.Windows.Forms.RadioButton'  " +
"  Style='{basedOn:`common1` }' />    " +
"  </blkx:BBoard>    " +
"</mpXaml>    ";

    }  // class
}      

Silverlight and WinRT

The above code can be also used in Silverlight and in WinRT.

See also

 

 

TBW The BLOCKS World 2015  hdolder.com srl
C30730
2015-06-26