Closed leecrossley closed 9 years ago
The webBrowser_Unloaded
method which detaches event handlers of plugin commands (to prevent memory leak on page navigation) clears ALL plugin callbacks (aka ResultHandlers).
This results in the callback being lost and a "Failed to locate callback for id" error.
I'm unable to see a workaround for this without modifying the BaseCommand or NativeExecution.
As a temporary workaround, the platforms\wp8\cordovalib\Commands\BaseCommand.cs
DetatchHandlers()
method could be replaced with:
public void DetachHandlers()
{
this.OnCommandResult = null;
this.OnCustomScript = null;
foreach (string callbackId in new List<string>(ResultHandlers.Keys))
{
if (!callbackId.ToLower().Contains("datetimepicker"))
{
RemoveResultHandler(callbackId);
}
}
}
For avoiding monkey patching you can save the handler in selectDate method of DateTimePicker.cs class an then readd that on ResultHandler dictionary before DispatchCommandResult, its work great for me:
public event EventHandler<PluginResult> mySavedHandler;
....
public void selectDate(string options)
{
....
if (ResultHandlers.ContainsKey(CurrentCommandCallbackId))
{
mySavedHandler = ResultHandlers[CurrentCommandCallbackId];
}
....
}
private void dateTimePickerTask_Completed(object sender, DateTimePickerTask.DateTimeResult e)
{
......
try {
if (!ResultHandlers.ContainsKey(CurrentCommandCallbackId))
{
ResultHandlers.Add(CurrentCommandCallbackId, mySavedHandler);
}
.....
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, result + ""));
}
}
Great - thanks! I've not it tested yet it but it looks good so I'll merge into master. Not sure why I didn't do this...
DispatchCommandResult -> ResultHandlers.ContainsKey(result.CallbackId) always fails, as ResultHandlers is an empty array :/