当询问页面请求方法是否为POST时,UWP WebView控件不会刷新

时间:2018-09-03 08:31:50

标签: webview uwp

UWP WebView控件具有a Refresh() method,在大多数页面上都可以正常工作,但是在POST通过导航(例如WebView本身内部的表单提交)导航到WebView之后直接调用该方法会引发{{1} }事件,但实际上从不执行任何刷新。

以下是一些最小化的XAML来说明问题:

NavigationStarted

以下是相关代码:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Button Grid.Row="0" Name="Refresh" Click="Refresh_Click" Content="Refresh" />
        <WebView Grid.Row="1" Name="BrowserView" />
    </Grid>
</Page>

作为测试,我创建并托管了一个非常小的PHP页面,其中包含以下内容,并将其托管在namespace App1 { public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); BrowserView.NavigationCompleted += BrowserView_NavigationCompleted; BrowserView.NavigationStarting += BrowserView_NavigationStarting; BrowserView.Navigate(new Uri("http://localhost/test.php")); } private void Refresh_Click(object sender, RoutedEventArgs e) { BrowserView.Refresh(); } private void BrowserView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args) { // Always called on first load and when refreshed (even for POST results) Debug.WriteLine($"Navigation starting for URI {args.Uri}"); } private void BrowserView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args) { // Not called when a POST result is being refreshed Debug.WriteLine($"Navigation completed"); } } }

http://localhost/test.php

一旦我将WebView指向此页面,就可以在初始加载时刷新页面,但是提交表单后,<!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <form method="POST"> <p>Page loaded at <?php echo date('H:i:s') . (!empty($_POST['in']) ? ' with text "'.htmlspecialchars($_POST['in']).'"' : '') ?></p> <input type="text" name="in" /> <input type="submit" /> </form> </body> </html> 会引发Refresh()事件,但根本不会导航页面(如测试PHP结果的时间戳指示不变),并且永远不会引发相应的NavigationStarting事件。

在Edge中访问同一页面时,尝试刷新POST结果时会向用户显示此警告:

The dialog Microsoft Edge shows to users when refreshing a page submitted by POST

选择重试将POST数据重新提交到服务器,并且选择“取消”的行为与我看到的类似(什么也不做)。 UWP WebView控件显然不会以任何方式模拟此对话框。

由于我的实际应用程序向用户显示了一个刷新按钮,因此无论他们最近是否提交了表单,我都需要使用该按钮来执行某些操作,或者我需要禁用刷新按钮,以免在用户单击时刷新按钮。按钮什么都不做。

目前,我唯一的解决方案(令人惊讶的是,它确实有效,但确实令人讨厌)是调用NavigationCompleted,然后在GoBack()事件处理程序中立即调用NavigationCompleted。 / p>

我可以选择允许我检测这种情况的选项(例如GoForward(),以便可以禁用“刷新”按钮),或者在最坏的情况下,某种结果/允许我向用户传达发生(未发生)情况以及原因的例外情况。

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,则希望在提交表单后刷新页面。

如果是这样,您可以检查表单是否已在php处理程序中提交。请检查以下线程:Checking if form has been submitted - PHP。然后,在php处理程序中,您可以调用javascript方法来刷新页面,如[HttpPost] public ActionResult ShowStudent(string studentCode, PrivateStudentModel newPrivateStudent) { // some actions return PartialView("_ShowStudent", viewModelName); // mention partial view & viewmodel name here }