Xamarin Forms应用程序在单击“导航按钮”期间抛出以下错误。
如果我在import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
var progressView: UIProgressView!
var websites = ["apple.com", "google.com"]
//loadView() func
override func loadView() {
//create an instance of WKWebView()
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
//get URL and make a load URLRequest
guard let url = URL(string: "https://www." + websites[0]) else { return }
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
//set BarButtonItem on the top right of page
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Open", style: .plain, target: self, action: #selector(openTapped))
//add UIToolbar items with UIBarButtonItems
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let reload = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
//add progressView
progressView = UIProgressView(progressViewStyle: .default)
progressView.sizeToFit()
let progressButton = UIBarButtonItem(customView: progressView)
//add items to toolbarItems array
toolbarItems = [progressButton, spacer, reload]
navigationController?.isToolbarHidden = false
//create actionSheet UIAlertActionController for bar button drop down
@objc func openTapped() {
let alert = UIAlertController(title: "Open new page!", message: nil, preferredStyle: .actionSheet)
for website in websites {
alert.addAction(UIAlertAction(title: website, style: .default, handler: openPage))
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
alert.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
present(alert, animated: true)
}
//handlers for each UIAlertAction
func openPage(action: UIAlertAction) {
guard let actionTitle = action.title else { return }
guard let url = URL(string: "https://" + actionTitle) else { return }
webView.load(URLRequest(url: url))
}
//set webView title to web page title
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
类中评论以下方法DisplaySoccerStatus();
,则该应用程序将运行。因此,我认为数据库中很少有记录为空会导致此问题?不确定,如何解决该问题?
SoccerDailyStatus
SQLite.SQLiteException: Could not open database file: /data/user/0/com.companyname.soccerapp/files/.config/soccerpep (CannotOpen
}
堆栈跟踪:
public partial class SoccerDailyStatus : ContentPage
{
private SQLiteConnection conn;
SoccerAvailability status;
public SoccerDailyStatus(SoccerAvailability soccerStatus)
{
InitializeComponent();
status = soccerStatus;
BindingContext = status;
//DisplaySoccerStatus();
}
protected override async void OnAppearing()
{
conn = DependencyService.Get<Isqlite>().GetConnection();
conn.CreateTable<SoccerAvailability>();
base.OnAppearing();
async Task DisplaySoccerStatus()
{
var datetoday = DateTime.Now.ToString("ddMMyyyy");
//List<SoccerAvailability> myList = (from x in conn.Table<SoccerAvailability>() select x).ToList();
List<SoccerAvailability> myList = (from x in conn.Table<SoccerAvailability>().Where(x => x.CurrentDate == datetoday) select x).ToList();
if (myList != null)
{
SoccerAvailability soccerAvailability = new SoccerAvailability();
soccerAvailability.SoccerStatus = myList[0].SoccerStatus;
soccerAvailability.CurrentDate = DateTime.Now.ToString("ddMMyyyy");
await Navigation.PushAsync(new SoccerDailyStatus(soccerAvailability) { });
}
else
{
await DisplayAlert("Notification", "Unable to proccess status", "Cancel");
}
}
await DisplaySoccerStatus();
}
}
答案 0 :(得分:0)
我认为问题是由于ctor中调用了async
方法DisplaySoccerStatus
造成的。
单个数据连接不能同时在两个或多个线程中使用。
我建议两件事:
DisplaySoccerStatus
中呼叫您的async onAppearing
,然后使用await DisplaySoccerStatus
。哦,请使用async Task DisplaySoccerStatus
而不是async void
,以便您知道任务何时完成。异步无效情况是一种“失控”:您启动了任务链,但并不关心它何时完成。函数返回时,您所知道的就是直到第一次等待的所有操作都已执行。第一次等待后的所有内容都将在将来无法访问的某个特定时间运行。