C# Xamarin在UWP应用程序can';无法加载url,但边缘浏览器和Android应用程序可以
我有一个C#Xamarin表单应用程序,它有一个非常简单的Xaml页面,其中包含一个WebView控件,它不会呈现我作为UWP应用程序运行时创建的HTML页面。这个HTML页面的前一个版本在UWP版本中运行良好,但我添加了一些异步JavaScript代码,可能还有其他一些有问题的JavaScript,现在我在Xamarin Forms UWP应用程序中只得到一个空白页面,没有任何呈现。同样的HTML页面在Edge浏览器中运行良好,并且作为Android Emulator中运行的Android应用程序运行,因此UWP中的WebView控件似乎存在问题,我在下面介绍了具体的HTML。我在UWP清单中打开了互联网和麦克风功能 我正在Visual Studio 2019中使用Xamarin Forms 4.7.0.968和Xamarin Essentials 1.5.3.2以及Microsoft.NETCore.Universal 6.2.10对此进行测试 [更新:遵循调试提示,现在看到一个错误][没有抛出错误或异常,因此我无法判断问题是什么。任何人都知道为什么下面的HTML页面无法在UWP应用程序中呈现,或者我可以做些什么来调试……我希望浏览器中有类似F12的工具,这样我就可以知道出了什么问题。] 在遵循(Nico-Zhu-MSFT)的调试技巧之后,我看到了一个错误,它指向createDirectLineSpeechAdapters()调用的参数。显然,UWP WebView不喜欢将async()函数作为参数传递给createDirectLineSpeechAdapters(): 这个应用程序的Android版本中的边缘浏览器和WebView控件都工作得很好,所以我试图理解为什么这对于WebView的UWP实现是一个问题 以下是不会使用以下XAML呈现的HTML代码:C# Xamarin在UWP应用程序can';无法加载url,但边缘浏览器和Android应用程序可以,c#,xamarin,xamarin.forms,uwp,C#,Xamarin,Xamarin.forms,Uwp,我有一个C#Xamarin表单应用程序,它有一个非常简单的Xaml页面,其中包含一个WebView控件,它不会呈现我作为UWP应用程序运行时创建的HTML页面。这个HTML页面的前一个版本在UWP版本中运行良好,但我添加了一些异步JavaScript代码,可能还有其他一些有问题的JavaScript,现在我在Xamarin Forms UWP应用程序中只得到一个空白页面,没有任何呈现。同样的HTML页面在Edge浏览器中运行良好,并且作为Android Emulator中运行的Android应用
<!DOCTYPE html>
<html>
<head>
<script crossorigin="anonymous"
src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<style>
html,
body {
height: 100%;
}
body {
margin: 0;
}
#webchat {
height: 100%;
width: 100%;
margin: 0;
}
</style>
</head>
<body>
<div id="webchat" role="main"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdn.botframework.com/botframework-webchat/latest/webchat-es5.js"></script>
<script>
(async function () {
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'webchat/join',
value: {
language: window.navigator.language
}
}
});
}
return next(action);
});
const settings = {
locale: 'en-us',
store,
userID: 'MyUId'
};
let adapters = null;
const fetchCredentials = async () => {
const region = 'eastus';
const response = await fetch(`https://${region}.api.cognitive.microsoft.com/sts/v1.0/issuetoken`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'Ocp-Apim-Subscription-Key': '<my key>'
}
});
if (!response.ok) {
throw new Error('Failed to fetch authorization token.');
}
const authorizationToken = await response.text();
return { authorizationToken, region };
};
adapters = await window.WebChat.createDirectLineSpeechAdapters({
fetchCredentials
});
window.WebChat.renderWebChat(
{
...adapters,
...settings
},
document.getElementById('webchat'));
document.querySelector('#webchat > *').focus();
})().catch(err => console.error(err));
</script>
</body>
</html>
html,
身体{
身高:100%;
}
身体{
保证金:0;
}
#网络帽{
身高:100%;
宽度:100%;
保证金:0;
}
(异步函数(){
const store=window.WebChat.createStore({},({dispatch})=>next=>action=>{
如果(action.type==='DIRECT\u LINE/CONNECT\u completed'){
派遣({
键入:“网络聊天/发送事件”,
有效载荷:{
名称:'webchat/join',
价值:{
语言:window.navigator.language
}
}
});
}
返回下一步(操作);
});
常量设置={
地点:“en us”,
商店,
userID:'MyUId'
};
设适配器=null;
const fetchCredentials=async()=>{
常量区域='东美国';
const response=await fetch(`https://${region}.api.cognitive.microsoft.com/sts/v1.0/issuetoken`{
方法:“POST”,
标题:{
“内容类型”:“应用程序/x-www-form-urlencoded;字符集=utf-8”,
“Ocp Apim订阅密钥”:”
}
});
如果(!response.ok){
抛出新错误('未能获取授权令牌');
}
const authorizationToken=wait response.text();
返回{authorizationToken,region};
};
adapters=Wait window.WebChat.createDirectLineSpeechAdapters({
获取凭据
});
window.WebChat.renderWebChat(
{
…适配器,
…设置
},
document.getElementById('webchat');
document.querySelector(“#webchat>*”).focus();
})().catch(err=>console.error(err));
以下是显示WebView外观的XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:windows="clr-namespace:Xamarin.Forms.PlatformConfiguration.WindowsSpecific;assembly=Xamarin.Forms.Core"
mc:Ignorable="d"
x:Class="Fasst.MainPage">
<StackLayout>
<StackLayout Orientation="Horizontal">
<Button Text="Assistant" Clicked="AssistantButton_Clicked" />
<Button Text="Map" Clicked="MapButton_Clicked" />
</StackLayout>
<BoxView x:Name="mapPane" IsVisible="false" Color="Blue" />
<!-- Place new controls here -->
<WebView x:Name="assistantPane" Source="https://fasstassistant-rhw.azurewebsites.net/Index" windows:WebView.IsJavaScriptAlertEnabled="true"/>
</StackLayout>
</ContentPage>
。我希望浏览器中有类似F12的工具,这样我就可以知道出了什么问题
要在UWP应用程序中调试WebView控件,可以参考以下内容
我们需要使用DOM Explorer来检查和调试WebView控件,您也可以使用它来测试这个UWP WebView控件。对于我的测试html页面,抛出issuetoken访问被拒绝。你能分享用于加载html内容的c代码吗?好的,我在原始问题中添加了该信息,我不知道,谢谢。issuetoken访问错误是意料之中的,因为该示例要求传递订阅密钥才能使调用成功。因此,请按照上面的步骤调试webview控件并共享您遇到的错误。好的,我按照调试步骤进行操作,并用结果更新了问题。知道它告诉我什么吗?哪个是webview加载html文件或源url?html是webview试图加载的网页。我是该HTML网页的作者,如果需要可以更改它。XAML是加载HTML页面的WebView。这就是你要问的吗?