Toast 通知不适用于 JavaScript 呼叫
Toast notifications are not working on a JavaScript call
我正在使用 Xamarin.Android 开发应用程序,我想在从 JavaScript 导出报告时显示 Toast 通知。我的应用调用报告并成功生成。但是,从不显示 Toast 通知。我发现只有当我在该特定行中设置断点时,它才会显示在 Visual Studio 2017.
这是我的 C# class 处理 JS 的一部分。
class CallJSInterface : Java.Lang.Object
{
private class Timetable
{
public string member { get; set; }
public string role { get; set; }
public string time { get; set; }
public string lastColor { get; set; }
}
private Context context;
public CallJSInterface(Context context)
{
this.context = context;
}
[Export]
[JavascriptInterface]
public void ExportToExcel(string results)
{
Toast.MakeText(context, context.GetString(Resource.String.LblExportMsg), ToastLength.Short).Show();
var timetable = JsonConvert.DeserializeObject<List<Timetable>>(results);
//Excel conversion
}
}
这是 LblExportMsg 在 Strings.xml:
中的值
<string name="LblExportMsg">Exporting your Agenda to Excel.</string>
另外,这是我在JS中调用函数的例子:
$("#linkDownload").click(function (e) {
e.preventDefault();
CSharp.ExportToExcel('[{"member":"Luis","role":"Timer","time":"00:15:15","lastColor":"red"},{"member":"Luis","role":"Timer 1","time":"00:15:00","lastColor":"green"},{"member":"Luis","role":"Timer 2","time":"00:15:17","lastColor":"red"},{"member":"Luis","role":"Timer 3","time":"00:07:15","lastColor":"green"},{"member":"Luis","role":"Timer 4","time":"00:23:15","lastColor":"red"},{"member":"Luis","role":"Timer 5","time":"00:15:15","lastColor":"green"},{"member":"Luis","role":"Timer 6","time":"01:15:15","lastColor":"yellow"},{"member":"Luis","role":"Timer 7","time":"00:18:15","lastColor":"green"},{"member":"Luis","role":"Timer 8","time":"00:15:22","lastColor":"green"}]');
});
此外,HTML 按钮:
<button type="button" id="linkDownload">Export</button>
最后,这就是我将 JS 界面从主视图添加到 WebView 的方式 activity:
webView.AddJavascriptInterface(new CallJSInterface(this), "CSharp");
有谁知道我做错了什么?它与上下文有关吗?我怎样才能检查它?感谢您的帮助。
PS:
- 最低 SDK 为 21,目标 SDK 27。
- 我正在使用 JSON.NET 进行反序列化。
我能够修复它,我在导出报告之前添加了一个超时,并且将过程一分为二,一个用于显示 Toast,另一个用于报告。
$("#linkDownload").click(function (e) {
CSharp.Alert(currentTranslation.lblExportMsg);
setTimeout(function () {
CSharp.ExportToExcel(JSON.stringify(results));
}, 250);
});
我正在使用 Xamarin.Android 开发应用程序,我想在从 JavaScript 导出报告时显示 Toast 通知。我的应用调用报告并成功生成。但是,从不显示 Toast 通知。我发现只有当我在该特定行中设置断点时,它才会显示在 Visual Studio 2017.
这是我的 C# class 处理 JS 的一部分。
class CallJSInterface : Java.Lang.Object
{
private class Timetable
{
public string member { get; set; }
public string role { get; set; }
public string time { get; set; }
public string lastColor { get; set; }
}
private Context context;
public CallJSInterface(Context context)
{
this.context = context;
}
[Export]
[JavascriptInterface]
public void ExportToExcel(string results)
{
Toast.MakeText(context, context.GetString(Resource.String.LblExportMsg), ToastLength.Short).Show();
var timetable = JsonConvert.DeserializeObject<List<Timetable>>(results);
//Excel conversion
}
}
这是 LblExportMsg 在 Strings.xml:
中的值<string name="LblExportMsg">Exporting your Agenda to Excel.</string>
另外,这是我在JS中调用函数的例子:
$("#linkDownload").click(function (e) {
e.preventDefault();
CSharp.ExportToExcel('[{"member":"Luis","role":"Timer","time":"00:15:15","lastColor":"red"},{"member":"Luis","role":"Timer 1","time":"00:15:00","lastColor":"green"},{"member":"Luis","role":"Timer 2","time":"00:15:17","lastColor":"red"},{"member":"Luis","role":"Timer 3","time":"00:07:15","lastColor":"green"},{"member":"Luis","role":"Timer 4","time":"00:23:15","lastColor":"red"},{"member":"Luis","role":"Timer 5","time":"00:15:15","lastColor":"green"},{"member":"Luis","role":"Timer 6","time":"01:15:15","lastColor":"yellow"},{"member":"Luis","role":"Timer 7","time":"00:18:15","lastColor":"green"},{"member":"Luis","role":"Timer 8","time":"00:15:22","lastColor":"green"}]');
});
此外,HTML 按钮:
<button type="button" id="linkDownload">Export</button>
最后,这就是我将 JS 界面从主视图添加到 WebView 的方式 activity:
webView.AddJavascriptInterface(new CallJSInterface(this), "CSharp");
有谁知道我做错了什么?它与上下文有关吗?我怎样才能检查它?感谢您的帮助。
PS:
- 最低 SDK 为 21,目标 SDK 27。
- 我正在使用 JSON.NET 进行反序列化。
我能够修复它,我在导出报告之前添加了一个超时,并且将过程一分为二,一个用于显示 Toast,另一个用于报告。
$("#linkDownload").click(function (e) {
CSharp.Alert(currentTranslation.lblExportMsg);
setTimeout(function () {
CSharp.ExportToExcel(JSON.stringify(results));
}, 250);
});