I’ve been posting so much about the awesome LightSwitch HTML client lately (have you seen the second season of Leading LightSwitch by the way?) that you might get the impression that I’m now an HTML-Hipster exclusively…
No way, and as a tribute to my love to the strongly typed world (also just because it’s really cool) I’m going to challenge your LightSwitch haxpert level with a coding riddle…
I have a normal LightSwitch screen, that has a local property (from Screen Designer > Add data item > Local property: UserRegistration(Entity) ) and a fancy custom user control. From the screen’ Created method, I’m trying to bind an event handler to an event on this fancy custom user control that is supposed to instantiate the local UserRegistration property, then show a modal popup. Nothing extraordinary, one would think when looking at the code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace LightSwitchApplication | |
{ | |
public partial class SchedulingViewDiagram | |
{ | |
partial void SchedulingViewDiagram_Created() | |
{ | |
this.FindControl("ReallyCoolImageButton").ControlAvailable += SchedulingViewDiagram_ControlAvailable; | |
} | |
void SchedulingViewDiagram_ControlAvailable(object sender, ControlAvailableEventArgs e) | |
{ | |
var reallyCoolImageButton = e.Control as ReallyCoolImageButton; | |
reallyCoolImageButton.Clicked += (s,evt ) => { | |
this.NewUserRegistration = this.DataWorkspace.ApplicationData.UserRegistrations.AddNew(); | |
this.NewUserRegistration.RegistrationAdded = DateTime.Now; | |
this.OpenModalWindow("NewRegistrationPopup"); | |
}; | |
} | |
} | |
} |
This code actually throws a NullReferenceException at line 15 (much to my own initial surprise)… I verified this by putting up a breakpoint, and indeed, ‘this.NewUserRegistration’ returns null even though we just assigned the return value of the AddNew method on line 14!!
Your mission, should you choose to accept it: explain why, and propose a fix. The rules of the challenge are simple: the first one to post the correct answer wins, he/she doesn’t actually win anything (I’m completely broke after giving away $10.000 last month), and you are excluded from entering the competition if your name is Matt Evans, Huy Nguyen, … ( or if you share a whiteboard with any of these guys).
As always, should any member of your team be caught or killed, the Secretary will disavow all knowledge of your actions.
Keep rocking LS!
Hi Jan
I’ll have a shot at the issue when I get some spare time, but am interested in your “fancy custom user control” for registrations.
Can you please share details on how to check it out.
Thanks in advance, Mark.
Pingback: LightSwitch hacking challenge: fix the NullReferenceException: sample | Jan Van der Haegen's blog
why not use
(e.Control as ReallyCoolImageButton).Click += (s,evt) => {
this.Details.Dispatcher.BeginInvoke(() =>
{
this.LocalStringProperty = “Hello world”;
this.ShowMessageBox(“Local string property is: ” + this.LocalStringProperty);
});
};
with this your able to use the background dispatcher where the LocalStringProperty is set.
Pingback: LightSwitch Community & Content Rollup– May 2013 (+more) - Beth Massi - Sharing the goodness - Site Home - MSDN Blogs
Pingback: Beth Massi的中文博客