Initialize the Application (Version V5)

The second parameter of “BuildApp” is used to initialize components and is done right after the configuration. Often it’s useful to split these two actions. Referring to our example we could use the initialize method to start the clock automatically after starting the application and after the configuring the “TimerClock” unit.
To implement this “feature” first we remove the wire to start the clock from our application definition. Because this wire definition is the only content of flow “StartClockFlow”, simply remove this flow completely. The remaining application definition is shown below:

$App Settings {
    $CodeGeneration {
        BinFolder = "..\..\AD4.AlarmClockSample.bin\Debug\"
        BinIncludeSubFolder = "True"
        CodeFolder = "..\..\AD4.AlarmClock\"
    }
}

$App GlobalStepDefinitions {
    $Step MainWindow {
        CustomName = "UI" PreparedInstance = "True" SingleInstance = "True" }
    $Step TimerClock { SingleInstance = "True" }
    $Step CalculateRemainTime { SingleInstance = "True" }
}

$Flow MainFlow {
    $Wire ShowCurrentTimeFlow?CurrentTime -> RaiseAlarmFlow?CurrentTime
}

$Flow ShowCurrentTimeFlow {
    $Wire TimerClock -> UI?CurrentTime
    $Wire TimerClock -> ?CurrentTime
}

$Flow SetAlarmTimeFlow {
    $Wire UI?AlarmDuration -> ConvertToDouble?StringInputPin
    $Wire ConvertToDouble -> CalculateAlarmTime
    $Wire CalculateAlarmTime -> CalculateRemainTime?AlarmTime
}

$Flow RaiseAlarmFlow {
    $Wire ?CurrentTime -> CalculateRemainTime?CurrentTime
    $Wire CalculateRemainTime -> DecideToRaiseAlarm
    $Wire DecideToRaiseAlarm -> SpecifyAlarmString
    $Wire SpecifyAlarmString -> UI?AlarmText
}

Next we recreate the source code of the application (by button “Code”). Now we are able to customize the UI and remove the code behind parts that are not used anymore. No error is shown by visual studio because of missing parts. The following screenshot shows the customized UI:

AD4.AlarmClockSample.MainWindow.V5.png

Here’s the remaining code behind part of the WPF form “MainWindow”:

/// <summary>Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
      public MainWindow()
      {
            InitializeComponent();
 
            this.StartAlarmClockButton.Click += (a, b) => AlarmDuration(this.AlarmClockDurationTextBox.Text);
      }
 
      public void AlarmText(string text)
      {
            if (this.CheckAccess())
                  this.AlarmClockTextBlock.Text = text;
            else
                  this.Dispatcher.Invoke(new Action<string>(AlarmText), text);
      }
 
      public void CurrentTime(DateTime currentTime)
      {
            if (this.CheckAccess())
                  this.CurrentTimeTextBlock.Text = currentTime.ToLongTimeString();
            else
                  this.Dispatcher.Invoke(new Action<DateTime>(CurrentTime), currentTime);
      }
 
      public event Action<string> AlarmDuration;
}

Now we customize the “TimerClock” class to be initialized. Simply implement the interface “AD4.AppDesigner.Core.Interfaces.IInitialize”. The following code snippet shows the customized code of “TimerClock.cs”:

using System;
using System.Threading;
using AD4.AppDesigner.Core.Interfaces;
 
namespace AD4.TimerClockLibrary
{
   public class TimerClock: IConfigure, IInitialize
   {
      private Timer _Timer;
      private int _Period;
 
      private void TimeElapsed(object state)
      {
         this.DefaultOutputPin(DateTime.Now);
      }
 
      public event Action<DateTime> DefaultOutputPin;
 
      public void Configure(object arg)
      {
         _Period = (int)arg;
      }
 
      public void Initialize(object arg)
      {
         _Timer = new Timer(new TimerCallback(TimeElapsed),null, 0, _Period);
      }
   }
}

That’s all. The application is ready to use after rebuilding the solution, reconstruction of the gluing code by pressing the button “Code” and rebuilding the solution again. (The first rebuild is to update the assemblies because the AD4.AppDesigner examines the assemblies by reflection and decides to create the Initialize method. This is also a source code. You have to rebuild to again to use the new source code.)

Note:
Currently, the calling of the initialize method is generated by the AD4.AppDesigner using reflection while generating the source code. It’s planned to improve this behavior in a future release. This means then you will not have to rebuild the application twice after implementing the Initialize interface.

By this chapter, the basic functionalities of the AD4.AppDesigner are explained. The following chapter demonstrates the advance features of the AD4.AppDesigner to improve the development process (focused on productivity).
Note:
You can download the solution here: AD4.TutorialSamples
Update (2014-08-13): Design attributes extended:
Update (2015-03-30): This page is obsolete. You find the current version of the tutorial as offline documentation included in downloads...

Previous: Configure the Application (Version V4)
Next: Usage Of Patterns

Last edited Mar 30, 2015 at 9:18 AM by InneHo, version 7