Friday, December 16, 2011

Hello World: Hooking events

I promised before I'd try to get this code up somewhere. I've uploaded this entire example to github here. Please read the notes in the Readme about deployment, it requires signing the package with a certificate you've added to your trusted root stores.

The app I discussed in my previous post is fairly boring: all it does is present some text to the screen. I wanted it to do a little bit more to experiment with how events are attached to controls in WinRT. How about I make the text change color when the mouse hovers over it? Actually I shouldn't say mouse, I mean Pointer. In Xaml apps, mouse, keyboard, and touch events are all unified into a Pointer events so they all operate in similar ways. Anyway, it is a simple addition that makes the app more interesting.

WinRTs notion of events is a little different from VCL, and I think it'll be a welcome change. Events are handled with multicast delegates, similar to in C#. With any given event for any control, you could add multiple delegates to listen for that event. Each event has an add_* and a remove_* method to install or uninstall an event listener. For my Text, I'll be adding PointerEntered and PointerExited events. Both of these require a Windows.UI.Xaml.Input.PointerEventHandler, so I'll create a little delphi wrapper for that class.

  TPointerEventHandler = class(TInspectableObject, PointerEventHandler)
    FProc: TProc ;
    constructor Create(Proc: TProc );
    // PointerEventHandler methods
    procedure Invoke(sender: IInspectable; e: IPointerEventArgs); safecall;

{ TPointerEventHandler }

constructor TPointerEventHandler.Create(
  Proc: TProc );
  FProc := Proc;

procedure TPointerEventHandler.Invoke(sender: IInspectable; e: IPointerEventArgs);
  FProc(sender,  e);
This creates an instance of a handler, and stores of a reference to a procedure that should handle the event. That means the code for my event handler can still live in TDerivedApp, even though it's handed off to WinRT by this helper class. I've added an OnPointerEntered and OnPointerExited methods to my application class. In my OnLaunched event is where I actually hook the event handler up to the code:
procedure TDerivedApp.OnLaunched(args: ILaunchActivatedEventArgs);
  insp: IInspectable;
  element: Windows_UI_Xaml_IUIElement;
  // Get the IWindowStatics
  OleCheck(RoGetActivationFactory(TWindowsString(SWindow), IWindowStatics, insp));
  WinStatic := insp as IWindowStatics;

  // Get an IXamlReaderStatics
  OleCheck(RoGetActivationFactory(TWindowsString(SXamlReader), IXamlReaderStatics, insp));
  ReaderStatic := insp as IXamlReaderStatics;

  WinStatic.Current.Content := ReaderStatic.Load(TWindowsString(content)) as IUIElement;

  // New code here: added event handlers for pointer enter and exit
  insp := (WinStatic.Current.Content as IFrameworkElement).FindName(TWindowsString('Text'));
  element := (insp as IUIElement);



procedure TDerivedApp.OnPointerEntered(sender: IInspectable; e: IPointerEventArgs);
  brush: ISolidColorBrush;
  insp: IInspectable;
  color: IInspectable;
  // Create a red brush
  OleCheck(RoGetActivationFactory(TWindowsString(SSolidColorBrush), ISolidColorBrushFactory, insp));
  OleCheck(RoGetActivationFactory(TWindowsString(SColors), IColorsStatics, color));
  brush := (insp as ISolidColorBrushFactory).CreateInstanceWithColor((color as IColorsStatics).Red);
  // Assign it to the text block
  (sender as ITextBlock).Foreground := (brush as IBrush);
I've omitted the code for OnPointerExited here; it's nearly identical to OnPointerEntered except it uses a white brush. Again, some of the code here, like creating a brush, is quite verbose and ugly; eventually class wrappers will make this look much nicer, similar to the C# and C++ projections that would alow this to look something more like:
  sender.Foreground := Windows.UI.Xaml.Media.SolidColorBrush.Create(


  1. It is time on behalf of Microsoft to equate these reasons to hearten extra developers to erect cell phone apps by targeting Windows Phone 8. Poker Uang Asli,

  2. This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.
    Seputar Dunia

  3. If you are looking for buy used cars here we offer the good condition, examined, finest, tested and good maintain buy used cars in Meerut.
    Buy Second Hand Cars in Meerut

  4. Situs Judi Online Poker terpercaya I very easily understand the contents of your articles because it uses a language that is easy to understand


  5. Pada permainan Poker Online, ada ber aneka macam meja jumlah pemain yang tersedia. Ada meja yang hanya untuk 3 pemain , 4 pemain dan bahkan sampai 9 pemain. Disini anda bisa pilih sesuai dengan keinginan anda apakah ingin bermain rame – rame atau hanya untuk sedikit pemain saja. Dan sudah banyak sekali orang yang mengemari Poker Online tersebut.
    BandarQ Online


  6. I think I need it. Thank you for update information. i like your blog.
    فني صحي الكويت
    نقل عفش بالكويت