C# InvokeScript gives error 80020006 - c#

I have the piece of code as follows.
webBrowser.IsScriptEnabled = true;
webBrowser.InvokeScript("eval", "alert('hey')");
It gives An unknown error has occurred. Error: 80020006. Could you guide how to rectify this error.

There is no built-in browser window.alert in Windows Phone, but you can bind one as follows to call WebBrowser.ScriptNotify
//inside the page
window.alert = function (__msg) { window.external.notify(' + __msg + '); };
// in your C# code
this.webBrowser.ScriptNotify += new EventHandler<NotifyEventArgs>(Browser_ScriptNotify);
void Browser_ScriptNotify(object sender, NotifyEventArgs e)
{
MessageBox.Show(e.Value);
}
//later
this.CordovaView.Browser.IsScriptEnabled = true;
this.CordovaView.Browser.InvokeScript("alert", "ok");
On Cordova, there is also a Notification Plugin that you can plug by
window.alert = navigator.notification.alert;
Just be sure to enable Notification Plugin in config.xml
<feature name="Notification">
<param name="wp-package" value="Notification"/>
</feature>

It is caused by race condition. What we need to do is
this.CordovaView.Browser.LoadCompleted += Browser_LoadCompleted;
then
void Browser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
this.CordovaView.Browser.IsScriptEnabled = true;
this.CordovaView.CordovaBrowser.IsScriptEnabled = true;
this.CordovaView.Browser.InvokeScript("setPushToken", push_uri);
}

Related

Generating Report in C# causing error

I'm generating report in C# by using background worker but I'm getting this error.
Source code as follows:
I have to access my datagridview Records to access it's data.
A small window opens up in my datagridview which asks user to enter date from and to generate report, then i access back my datagridview convert into data table write in XML file and generate report.
Global Variables
// This is the form where the data lies, I'm accessing it's instance.
Records TR = new Records();
// This is the form where report will be displayed.
TReportDisplay TRD = new TReportDisplay();
// This is the report.
Treport treport1 = new Treport();
private void button1_Click(object sender, EventArgs e)
{
// FIXED HERE - 1
// FIXED - 2 IN THE ANSWER BELOW.
// Accessing my DataGridView Form Instance.
TR = Application.OpenForms.OfType<Records>().ElementAt(0);
treport1.SetDataSource(TR.ds);
TRD.crystalReportViewer2.ReportSource = treport1;
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
TRD.crystalReportViewer2.ReportSource = treport1;
ParameterFieldDefinitions Parameters;
ParameterFieldDefinition Parameter;
ParameterValues Values = new ParameterValues();
ParameterDiscreteValue DiscreteValue = new ParameterDiscreteValue();
DiscreteValue.Value = dateTimePicker1.Text;
Parameters = treport1.DataDefinition.ParameterFields;
Parameter = Parameters["fromdate"];
Values = Parameter.CurrentValues;
Values.Clear();
Values.Add(DiscreteValue);
Parameter.ApplyCurrentValues(Values);
DiscreteValue.Value = dateTimePicker2.Text;
Parameters = treport1.DataDefinition.ParameterFields;
Parameter = Parameters["todate"];
Values = Parameter.CurrentValues;
Values.Add(DiscreteValue);
Parameter.ApplyCurrentValues(Values);
}
}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Message"); };
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
TRD.ShowDialog();
}
There were two issues, first of all updating progress bar from a different thread for which I got answer, another calling form instance after background worker was causing the issue, just put the variable before background worker start async and fixed.
The error message is telling you that the only way to update the controls is via the thread that the controls are running on. You are currently running on a different thread (the one for the back ground worker).
Take a look at the example in this link for another question on SO Invoke(Delegate) . You basically should have a method that you can call, to update the UI, which can check if it is on the correct thread and if it is not gets the correct thread to call it.
This is a snippet of code that was on the link parvee gave above that shows how you can do this.
public void UpdateProgress(int percentComplete)
{
if (!InvokeRequired)
{
ProgressBar.Value = percentComplete;
}
else
{
Invoke(new Action<int>(UpdateProgress), percentComplete);
}
}

Atapi CallInfoChangeEvent not fired

I am using the TAPI 2.0 wrapper from JulMar (https://atapi.codeplex.com/) and I'm having trouble with it.
The Initialization
void initTAPI()
{
myTAPI = new TapiManager("GetCaller");
if (!myTAPI.Initialize())
{
MessageBox.Show("FAILED!");
}else
{
name = myTAPI.Lines[0].Name;
lineName = (myTAPI != null && myTAPI.Lines.Length > 0 ? name : string.Empty);
foreach(TapiLine line in myTAPI.Lines)
{
line.NewCall += this.OnNewCall;
line.Ringing += this.OnRinging;
line.CallStateChanged += this.OnCallState;
line.CallInfoChanged += this.OnCallInfo;
}
MessageBox.Show(lineName);
}
}
So I get the lineName. When I now dial a number through the program, it fires
OnCallState
private void OnCallState(object sender, CallStateEventArgs e)
{
if (InvokeRequired == true)
{
this.BeginInvoke(new EventHandler<CallStateEventArgs>(this.OnCallState), new object[] { sender, e });
return;
}
label1.Text = "Outgoing Call...";
}
But what I actually want to do is to get the number of an incoming call, but OnCallInfo does not get fired.
OnCallInfo
private void OnCallInfo(object sender, CallInfoChangeEventArgs e)
{
if (InvokeRequired == true)
{
this.BeginInvoke(new EventHandler<CallInfoChangeEventArgs>(this.OnCallInfo), new object[] { sender, e });
return;
}
label1.Text = "Incoming Call...";
}
It says somehwere, that it only works with x86, so I changed the target but still no success.
PS: I have a call manager (ProCall) installed on the same machine, that tells me when someone calls, so I should be able to get the info in c# as well?
Here is the whole code if someone is interested: http://pastebin.com/Q5W5iGun
Depending on TSP, you may get call info messages, but TAPI does not force the driver to do this. So some TSP make you get the info yourself. In the Win32 API this is done via lineGetCallInfo.
After a quick look in this atapi wrapper, this happens in the GatherCallInfo method of the TapiCall class. However I can see no way to trigger this manually in this wrapper. You would need to modify the atapi source to make this a public method.
You can use example from TAPI which do the same. The only difference is new line.Monitor() method
foreach (TapiLine line in tapiManager.Lines)
{
try
{
line.NewCall += OnNewCall;
line.CallStateChanged += OnCallStateChanged;
line.CallInfoChanged += OnCallInfoChanged;
line.Monitor();
}
catch (TapiException ex)
{
LogError(ex.Message);
}
}
For further reading read this https://atapi.codeplex.com/SourceControl/latest#Atapi/trunk/source/test/TcMon/TapiMonitorForm.cs

ShareOperation.ReportCompleted() results in exception being thrown in UWP

I am trying to handle the the Share Operation
Code:
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
ShareOperation shareOperation = args.ShareOperation;
Uri uriReceived = null;
if (shareOperation.Data.Contains(StandardDataFormats.WebLink))
uriReceived = await shareOperation.Data.GetWebLinkAsync();
shareOperation.ReportCompleted();
}
It crashes at shareOperation.ReportCompleted(); showing error message as
"There was no match for the specified key in the index."
I tried searching for this error landing me to this question, But it seemed it was a problem that went away with later builds, now I'm facing this issue how do you recommend I handle it.
According to the Report sharing status parts of Receive data,
As a result, you shouldn't call it unless your app is at a point where it can be dismissed by the user.
I guess the reason for the exception is the reporting actions require user's permissions. If you call the shareOperation.ReportCompleted(); in ShareTargetActivated directly you will skip the user's authorization. It seems like it is not allowed.
For the workaround, you can handle the code shareOperation.ReportCompleted(); in a function like Button_Click or OnGotFocus . The following code example can resolve your issue.
App.xaml.cs code:
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
rootFrame.NavigationFailed += OnNavigationFailed;
Window.Current.Content = rootFrame;
}
rootFrame.Navigate(typeof(MainPage), args.ShareOperation);
Window.Current.Activate();
}
MainPage.xaml.cs code:
ShareOperation shareOperation;
protected override async void OnGotFocus(RoutedEventArgs e)
{
Uri uriReceived = null;
if (shareOperation.Data.Contains(StandardDataFormats.WebLink))
uriReceived = await shareOperation.Data.GetWebLinkAsync();
this.shareOperation.ReportCompleted();
base.OnGotFocus(e);
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
this.shareOperation = (ShareOperation)e.Parameter;
}
More details please reference the official sharetarget sample.

Phone Call Task Error on WP8

I have a problem with debug my app.There is no eror, I debug the app on device it start's but when I tap the button debug error!
that's how i use it:
In .xaml file:
<Button Name="taxi1" Content="Call Taxi 1" Click="taxi1_Click" />
in .xaml.cs file:
private void taxi1_Click(object sender, RoutedEventArgs e)
{
PhoneCallTask call = new PhoneCallTask();
call.DisplayName = "Calling Taxi 1";
call.PhoneNumber = "1411111";
call.Show();
}
and i added:
using Microsoft.Phone.Tasks;
I think what I'm doing it wrong is that i use this function for several times the only thing that is changing is the name of taxi:
private void taxi2_Click (....)
and the call.DisplayName="Calling Taxi 2";
and call.PhoneNumber = "1422222";
Maybe I should use only one function like
private void PhoneCall_Click(object sender, RoutedEventArgs e)
{
string number="";
HttpUtility.UrlEncode((sender as Button).Tag.ToString());
if (NavigationContext.QueryString.TryGetValue("number", out number))
{
if (!string.IsNullOrWhiteSpace(number))
{
PhoneCallTask call = new PhoneCallTask();
call.DisplayName = "Taxi";
call.PhoneNumber = number;
call.Show();
}
}
}
and <Button ....... Tag="141111" .. />
just one thought, do you have permission added in manifest for this to work ? if so, writing what error is actually being thrown would be helpful in order to help you
ID_CAP_PHONEDIALER should be added

handle "The operation completed successfully" error

I have a vast application running WPF and I occasionally get the
The operation completed successfully
error randomly, could be a whole host of things.
Is there any way to trap this code and just restart the app.
I'm already using
#region "Error Checking"
void OnAppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
HandleError("OnAppDomainUnhandledException", e.ExceptionObject.ToString(), e.ExceptionObject.ToString());
}
void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
string error = string.Empty;
if (e.Exception.InnerException != null)
error = e.Exception.InnerException.Message;
HandleError("OnDispatcherUnhandledException", e.Exception.Message.ToString(),error);
e.Handled = true;
}
#endregion
but this does not appear to catch/handle the error
When I Google it seems that it has something to do with SplashScreens...
Maybe this may help you, or this.

Categories

Resources