Hello World C# Demo

NOTE: this example has been updated with the Version 5.0.0 improvements. Also, see CPSBaseWindow.


Since the Demo App has gotten rather complex, I went ahead and created a simple Hello World example with an image button. Clicking the button will hide or show the Hello World label. The CPSBaseWindow contains a Canvas and an Adorner Decorator by default. This is so you could add Dialogs to the Canvas at a z-index, or add validation rectangles on textboxes via the AdornerDecorator. For more information on AdornerDecorator: AdornerDecorator Example (Field Validation)

This example only requires Faster WPF, Aero, and Rx .dll's.

Faster WPF has been split into two separate .dll's:
(1) FasterWPF which has the core
(2) optional FasterWPFExtras which supports the WPF Extended Toolkit, Thriple, and Xceed DataGrid

Also, be aware that you need to show the MainWindow from App.xaml.cs:
public partial class App : Application
     protected override void OnStartup(StartupEventArgs e)

            MainWindow mn = new MainWindow();
You may delete any default Window.xaml and Window.xaml.cs, replacing these with just a single .cs file for MainWindow. Also, make sure to remove StartupUri="MainWindow.xaml" from the App.xaml.

Here are the references:


On the Windows 8 desktop, the legacy Win32 side, follow these tips:
(1) when you add the reference to PresentationFramework.Aero.dll in the solution, make sure to set Copy Local to True,
(2) use Configuration Manager to set the target platform to x86 since that's what I targeted when I compiled the dll's, it's not enough to set the target platform in the Build properties of each Project,

Here is the code:

#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FasterWPF;

namespace TestComposites
    public partial class MainWindow : CPSBaseWindow
        Canvas cnvs = null;
        Grid mainGrid = null;

        public MainWindow()
            base.StartInitializeComponent(Brushes.Silver, new Thickness(2));

            this.Title = "Main Window";

            cnvs = base.Canvas;
            cnvs.Background = Brushes.WhiteSmoke;
            Application.Current.Properties["Canvas"] = cnvs;

            string cnvsguidAdorner = base.AdornerDecoratorGuid;
            mainGrid = new Grid();

            //Add Main Grid to Window (by adding it to the Window's internal AdornerDecorator)
            base.AddMainParentToWindow(adornerDecorator =>
                               .AddExisting<Grid, AdornerDecorator>(0, 0, mainGrid)
                               .EndComposite<AdornerDecorator, AdornerDecoratorArgs>(null);

            //4. Main Grid - each child will be added to the grid wrapped inside a DockPanel Composite
            mainGrid.Initialize(500D, 500D, 23D, ContainerType.DockPanel, 4, 3, 1.0, GridUnitType.Star, 1.0, GridUnitType.Star, 100D, GridUnitType.Pixel);
            mainGrid.ShowGridLines = true;
            mainGrid.Visibility = Visibility.Hidden;

            Application.Current.Properties["MainGrid"] = mainGrid; //store off a global handle to MainGrid

            Grid flatImageButton = null;
            Brush darkBlue = BrushExt.CreateSolidColorBrushFromString("#FF002050");

            //************   SETTINGS FOR ITEMS ON THE MAIN GRID  *******
                .SetItemBorderSettings<Grid>(double.NaN, double.NaN, Brushes.Silver, new Thickness(2))
                .SetFontOnLabel(0, 0, "Segoe UI", 20, FontWeights.Bold, FontStyles.Normal)
                .Set<TextBox, Grid>("HorizontalAlignment", System.Windows.HorizontalAlignment.Stretch)
                .Set<DatePicker, Grid>("HorizontalAlignment", System.Windows.HorizontalAlignment.Stretch)
                .Set<Rectangle, Grid>("Height", 50D)
                .Set<Label, Grid>("Padding", new Thickness(5, 25, 5, 5))
                .SetHorizontalAlignmentForLabel(0, 0, System.Windows.HorizontalAlignment.Center)

            //************   ADD HELLO WORLD LABEL TO MAIN GRID *******
            Border helloWorldLabel = mainGrid.BeginComposite()
                   .AddLabel(0, 0, "Hello World", darkBlue, Brushes.White)
                   .EndComposite(new GridArgs(0, 1));

            //************   ADD FIELDS   *******
                   .AddLabel(1, 0, "First Name", darkBlue, Brushes.White)
                   .AddAnything<TextBox, Grid>(1, 1)
                   .AddAnything<Rectangle, Grid>(2, 2)
                   .AddLabel(1, 3, "Last Name", darkBlue, Brushes.White)
                   .AddAnything<TextBox, Grid>(1, 4)
                   .AddAnything<Rectangle, Grid>(2, 5)
                   .AddLabel(1, 6, "Date", darkBlue, Brushes.White)
                   .AddAnything<DatePicker, Grid>(1, 7)
                   .AddAnything<Rectangle, Grid>(2, 8)
                   .EndComposite(new GridArgs(1, 1));
            // FLAT IMAGE BUTTON 1 - Geometry examples sourced from Syncfusion Metro Studio 2 - copyright free icons
            string iconPath = @"M5.2010002,14.812L5.2010002,43.573997 56.098,43.573997 56.098,14.812z M29.868601,3.8869994C28.026201,3.8869996 26.534,5.3791947 26.534,7.2190399 26.534,9.0588851 28.026201,10.553 29.868601,10.553 31.7085,10.553 33.202,9.0588851 33.202,7.2190399 33.202,5.3791947 31.7085,3.8869996 29.868601,3.8869994z M19.200649,3.8869994C17.359457,3.8869996 15.867001,5.3791947 15.867001,7.2190399 15.867001,9.0588851 17.359457,10.553 19.200649,10.553 21.042044,10.553 22.536999,9.0588851 22.536999,7.2190399 22.536999,5.3791947 21.042044,3.8869996 19.200649,3.8869994z M8.5339746,3.8869994C6.6931,3.8869996 5.2010002,5.3791947 5.2010002,7.2190399 5.2010002,9.0588851 6.6931,10.553 8.5339746,10.553 10.37495,10.553 11.867,9.0588851 11.867,7.2190399 11.867,5.3791947 10.37495,3.8869996 8.5339746,3.8869994z M0,0L63.999001,0 63.999001,48.792999 0,48.792999z";
            flatImageButton = FlatButtonHelpers.CreateFlatGeometryButton(iconPath, FlatButtonHelpers.GeometryButtonSize.Large, BrushExt.CreateSolidColorBrushFromString("#FF002050"), BrushExt.CreateSolidColorBrushFromString("#FFFFFFFF"), Brushes.Black, Brushes.WhiteSmoke, Brushes.Gray, new Thickness(1), new Thickness(1), "Toggle Hello World Label",
                 (o, ev) => { //do nothing on mouseover 
                 (o, ev) => { //toggle visibility on click
                     helloWorldLabel.Visibility = (helloWorldLabel.Visibility == Visibility.Visible) ? Visibility.Collapsed : Visibility.Visible;
            //************   ADD FLAT IMAGE BUTTON TO MAIN GRID  ******
            Border imgButtonComposite = mainGrid.BeginComposite()
                   .AddExisting(0, 0, flatImageButton)
                   .EndComposite(new GridArgs(0, 0));

            DockPanel dockPanelWithImgButton = mainGrid.GetContainerFromComposite<DockPanel, Grid>(imgButtonComposite);
            dockPanelWithImgButton.Background = Brushes.LightGray;

            Application.Current.Properties["ResizeCorrectly"] = new Action<object, RoutedEventArgs>((o, ev) => { ResizeCorrectly(); }); // Window Re-Sized Hook
            this.WindowState = WindowState.Maximized;
            mainGrid.Visibility = Visibility.Visible;

        protected override void ResizeCorrectly()
            if ((base.WindowWidth-10) > 0)
                //re-size Canvas
                FrameworkElement handleToCanvas = (FrameworkElement)Application.Current.Properties["Canvas"];

                if (handleToCanvas != null)
                    handleToCanvas.Height = base.WindowHeight;
                    handleToCanvas.Width = (base.WindowWidth - 10);

                //re-size Main Grid
                FrameworkElement handleToGrid = (FrameworkElement)Application.Current.Properties["MainGrid"];
                if (handleToGrid != null)
                    handleToGrid.Height = base.WindowHeight;
                    handleToGrid.Width = (base.WindowWidth - 10);

    }//end of class
}//end of namespace

NOTE: The ResizeCorrectly() approach isn't really necessary. You could leverage regular WPF layout techniques to resize on Window resizing. I just add it even in a Hello World because my whole approach is one of having fine-grained control over every element on the screen at all times.

Last edited Mar 24, 2014 at 5:51 AM by stagathome0069, version 30


No comments yet.