[英]Xamarin Forms - Webview not showing up
我正在開發一個小型的Xamarin.Forms webview應用程序。 這是對之前回答的問題的后續問題, xamarin-forms-making-webview-go-back
所以我有一個工具欄和一個后退按鈕實現和工作。 但是當我運行程序時,模擬器已經打開(使用Genymotion),程序運行並顯示工具欄和后退按鈕......但不會顯示webview。
但這是一件奇怪的事情,有時我在模擬器處於睡眠模式時運行程序,然后將程序切換回程序完美。 此外,當我在iOS上測試它時,它只是顯示工具欄而沒有webview! 更常見的是,模擬器不會顯示webView。 我也在我的Android設備上測試了這個,同樣的事情發生了,它會顯示工具欄但不顯示webview。
Abit令人困惑我知道但是任何人都可以幫助我解決這個問題。
我將在下面附上我的代碼:
App.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
namespace WebView_form
{
public class App : Application
{
public App()
{
//const string URL = "http://www.google.com";
MainPage = new NavigationPage(new WebPage());
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
WebPage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using Xamarin.Forms;
namespace WebView_form
{
public class WebPage : ContentPage
{
private WebView webView;
public WebPage()
{
webView = new WebView
{
Source = "https://www.google.com"
};
// toolbar
ToolbarItems.Add(new ToolbarItem("Back", null, () =>
{
webView.GoBack();
}));
Content = new StackLayout
{
Children = { webView }
};
}
}
}
如果有人能幫助我,那就太好了。
將VerticalOptions
設置為FillAndExpand
並對HorizontalOptions
執行相同操作(如果它不起作用)。
可能WebView
高度為零,因為當布局發生時,視圖仍然是空的。
所以像這樣更改WebPage.cs中的代碼;
// ... Other code
public WebPage()
{
webView = new WebView
{
Source = "https://www.google.com",
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand
};
// toolbar
ToolbarItems.Add(new ToolbarItem("Back", null, () =>
{
webView.GoBack();
}));
Content = new StackLayout
{
Children = { webView }
};
}
// ... Other code
另一件需要考慮的事情是:如果您正在響應WebView.Navigating事件,請確保如果加載的頁面是WevView.Source,則不要將其args.Cancel設置為true。 WebView.Navigating的iOS實現在加載WebView.Source但Android實現沒有加載時觸發。 如果在有問題的頁面是WebView.Source時將args.Cancel設置為true,則WebView將為空白。
將其設置為Info.plist以繞過App Transport Security檢查。 有關詳細信息,請訪問https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity 。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
注意:我的網址已經是HTTPS,不知道為什么它仍然被阻止。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.