CPSBaseWindow

CPSBaseWindow is new as of Version 5.0.

This provides a base class that your main Window may inherit from. Make sure to add code to App.xaml that may construct your Window. For instance, in this example, I "new up" my MainWindow which inherits from CPSBaseWindow and show it:

  public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            MainWindow mn = new MainWindow();
            mn.Show();
        }
    }

Also, make sure to remove StartupUri="MainWindow.xaml" from the App.xaml.
<Application x:Class="WPFContribDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"             
>
<!--StartupUri="MainWindow.xaml"-->

Advantages this base class provides:

1. Can get rid of the default Window.xaml. Just delete the old Window.xaml and Window.xaml.cs and then add your own Window that inherits from CPSBaseWindow.
Make sure your Window has a public constructor. At the beginning of this constructor, call base.StartInitializeComponent(). At the end of the constructor call base.EndInitializeComponent. There is no need for the normal InitializeComponent method since there is no XAML but include these new Start and End method calls instead.

2. Provides a built-in base.Canvas. This may be combined with the DialogHelpers static class in the FasterWPF.Dialogs namespace to display various Dialogs. This may be especially useful for Single-Page-Applications (SPA).

3. Provides an explicit, built-in base.AdornerDecorator and base.AdornerDecoratorGuid. This may be combined with the methods in the static class AdornerDecoratorExt. These methods allow adorning controls with validation rectangles when validating field values.

4. Provides built-in hooks to the Win32 GetWindowRect in order to provide convenience properties: base.WindowHeight and base.WindowWidth.

5. Provides built-in OPTIONAL method for manually re-sizing controls upon the Window Resize Event by overriding ResizeCorrectly; for example:

//this is an OPTIONAL method for manually re-sizing controls upon Window Resize Event
protected override void ResizeCorrectly()
{
    Double windowHeight = base.WindowHeight;
    Double windowWidth = base.WindowWidth - 10;

    if (windowWidth > 0)
    {
         //get a handle to some Framework Element and set its Width and Height
         //to automatically be re-sized whenever the Window is re-sized.
     }
}
Typically, the rich WPF layout controls offer enough re-sizing options, but in case you ever feel painted into a corner, you may find the above approach a straightforward way to implement custom re-sizing?

6. Provides a built-in method base.AddMainParentToWindow for adding a primary Parent to the Window via adding it to the built-in AdornerDecorator. For example:

base.AddMainParentToWindow(adornerDecorator =>
{
     adornerDecorator.BeginComposite<AdornerDecorator>()
                    .AddExisting<Grid, AdornerDecorator>(0, 0, mainGrid)
                    .EndComposite<AdornerDecorator, AdornerDecoratorArgs>(null);
});
So, you will end up with an object graph like this: Window -> Canvas -> AdornerDecorator -> Main Parent.

Last edited Mar 20, 2014 at 1:04 AM by stagathome0069, version 30

Comments

No comments yet.