I want to dynamically load forms at runtime, anytime during the lifecycle of the application from a HTTP result (the forms are loaded from a web server).
They can change at any time.
I’ve tried loading GlideLoader.LoadWindow() in other places besides the ProgramStarted() method, and the form does load, but any events that I hook up throw this exception:
“An unhandled exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll”
When ran in ProgramStarted() everything runs fine.
Here’s the code I call when I get the window xml from the Http Request:
GlideTouch.Initialize();
// Resize any loaded Window to the LCD's size.
Glide.FitToScreen = true;
// Load the Window XML string.
// Resources.GetString(Resources.StringResources.Window)
window = GlideLoader.LoadWindow(windowXml);
// Assign the Window to MainWindow; rendering it to the LCD.
InitWin(); // set up the form events
Glide.MainWindow = window;
You’ll need to have a web site that hosts the Window.txt file.
Here is the total Sample Code:
using System;
using System.Threading;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.Display;
using GHIElectronics.NETMF.Glide.UI;
using Gadgeteer.Networking;
namespace GadgeteerApp1
{
public partial class Program
{
static GHIElectronics.NETMF.Glide.Display.Window window;
void ProgramStarted()
{
// This does not work (set up network, HTTP call to get Window.txt)
// On this way, clicking the "toggleBtn" button throws exception:
// "An unhandled exception of type 'System.NullReferenceException' occurred in Microsoft.SPOT.TinyCore.dll"
SetUpNetworking();
// this does work (comment out the above line, uncomment the next line)
//SetUpGlide(Resources.GetString(Resources.StringResources.Window));
}
private void SetUpGlide(string windowXml)
{
// set up window xml and events
GlideTouch.Initialize();
Glide.FitToScreen = true;
window = GlideLoader.LoadWindow(windowXml);
// Events
camera.PictureCaptured += new Gadgeteer.Modules.GHIElectronics.Camera.PictureCapturedEventHandler(camera_PictureCaptured);
Button toggleBtn = (Button)window.GetChildByName("toggleBtn");
toggleBtn.TapEvent += new OnTap(toggleBtn_TapEvent);
Glide.MainWindow = window;
}
#region SETUP NETWORKING
private void SetUpNetworking()
{
ethernet_J11D.Interface.Open();
ethernet_J11D.Interface.NetworkInterface.EnableDhcp();
ethernet_J11D.Interface.NetworkAddressChanged += new GHI.Premium.Net.NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Interface_NetworkAddressChanged);
if (!ethernet_J11D.Interface.IsActivated)
{
GHI.Premium.Net.NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D.Interface);
}
}
void Interface_NetworkAddressChanged(object sender, EventArgs e)
{
// once we have a network address, ask the HTTP for the Window.txt file
Debug.Print(ethernet_J11D.Interface.NetworkInterface.IPAddress);
HttpRequest r = WebClient.GetFromWeb("http://mymachine.mdomain.local/Window.txt");
r.ResponseReceived += new HttpRequest.ResponseHandler(r_ResponseReceived);
}
void r_ResponseReceived(HttpRequest sender, HttpResponse response)
{
// when we get the window.txt file, set up the GLIDE window
sender.ResponseReceived -= r_ResponseReceived;
string rtext = response.Text;
Debug.Print(rtext);
SetUpGlide(rtext);
}
#endregion
public void toggleBtn_TapEvent(object sender)
{
camera.TakePicture();
}
void camera_PictureCaptured(Gadgeteer.Modules.GHIElectronics.Camera sender, Gadgeteer.Picture picture)
{
Image image = (Image)window.GetChildByName("webcam");
image.Bitmap = picture.MakeBitmap();
image.Invalidate();
}
}
}
No line number at all. Just the “An unhandled exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll”, in a “Microsoft Visual Studio” dialog window with the buttons “Break”, “Continue” , “Ignore”.
If I choose “Break” from the exception popup window, I get a “No Source Available” window
if I choose Continue, I get the same “Microsoft Visual Studio” exception dialog again
Uncaught exception
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
#### Message:
#### Microsoft.SPOT.Application::OnEvent [IP: 0090] ####
#### Microsoft.SPOT.EventSink::ProcessEvent [IP: 0023] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####
A first chance exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll
An unhandled exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll
Mike: Correct, there are no line numbers at all in the call stack.
Architect:
I Tried the dispatcher.
CurrentDispatcher.CheckAccess() always runs the “SetUpGlide(rtext)” method (it is saying a Dispatcher is not required).
I skipped the CheckAccess and always ran the dispatcher, and it throws the same Unhandled Exception as before.
As I was about to give up, I decided to just try moving things around until it worked.
I moved the GlideTouch.Initialize() to the ProgramStarted method. Aparently this cannot (or must be dispatched?) be called elsewhere.
Here is what worked:
[quote]void ProgramStarted()
{
// Moving these two lines from SetUpGlide() to ProgramStarted() is what let things work correctly.
GlideTouch.Initialize();
Glide.FitToScreen = true;
SetUpNetworking();
}[/quote]
Thanks to everyone for the help given. I would have given up much earlier had it not been for you guys.
Brian