Xamarin forms Binded Webview 显示在等待任务的 Android 但不显示在 IOS

Xamarin forms Binded Webview shows on Android but not on IOS from an awaited task

我创建了一个新的 Xamarin Forms Prism 项目来复制我在实际应用中遇到的这个问题。我想转到我的 data/web 服务并为我的应用程序的首页获取公告。它只是一个 HTML 字符串,所以我使用的是 WebView,我的 MainPage.xaml 看起来像 .

<?xml version="1.0" encoding="utf-8" ?>

<StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<Button Text="Go to page 1" Command="{Binding NextPageCommand}"/>
<WebView x:Name="webView"  WidthRequest="1000" HeightRequest="1000" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
    <WebView.Source>
        <HtmlWebViewSource Html="{Binding Announcements}"/>
    </WebView.Source>
</WebView>
</StackLayout>

还有我的 ViewModel

using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlankApp1.ViewModels
{
    public class MainPageViewModel : ViewModelBase,INavigatedAware
    {
    public MainPageViewModel(INavigationService navigationService) 
    : base (navigationService)
    {
        Title = "Main Page";
        NextPageCommand = new DelegateCommand(NextPage);
    }
    public DelegateCommand NextPageCommand { get; }
    private string _announcements;
    public string Announcements
    {
        get { return _announcements; }
        set { SetProperty(ref _announcements, value); }

    }
    private async void NextPage()
    {
        await NavigationService.NavigateAsync("Page1");

    }
    public async void OnNavigatedTo(NavigationParameters parameters)
    {

        if (Announcements == null)
        {
            Announcements = "<html><body>" + "Working Shows this HTML in the Webview" + "</body></html>";
        }
        else
        {
            Announcements = "<html><body>" + "Not Working, Didnt update with this new HTML" + "</body></html>";
        }

    }        
    public async void OnNavigatedFrom(NavigationParameters parameters)
    {
        //throw new NotImplementedException();
    }
}

}

在 OnNavgatedTo 函数中,它不会更新当您离开此页面导航然后 return.

时显示的 HTML

如果有人知道为什么这在 android 上可以正常工作但在 iOS 上不能正常工作,请告诉我。我已经看了 2 天了,但仍然无法像 Android

上那样显示它

老实说,我真的不明白你想用 Task.Run 做什么。

就这样:

public async void OnNavigatedTo(NavigationParameters parameters)
{
    try
    {
        //GetAnnouncements
        Announcements = "<html><body>" + await App.dataService.GetAnnouncements() + "</body></html>";
    }
    catch (System.OperationCanceledException ex)
    {
        Console.WriteLine($"announcement load cancelled: {ex.Message}");
    }

此外,我认为最新版本的 Prism 具有基于任务的导航方法。

更新:

您更新了您的问题。 我怀疑您的问题是您没有导航至 MainPage,而是导航至 MainPage = new MainPage()。因此 OnNavigatedTo 永远不会被调用并且 Announcement 永远不会被设置。

如果要更改 WebView 的内容,请尝试绑定其 HtmlWebViewSource 而不是 Html

在视图模型中创建 HtmlWebViewSource 属性:

private HtmlWebViewSource _webviewSource;
public HtmlWebViewSource WebviewSource
{
    get { return _webviewSource; }
    set
    {
        SetProperty(ref _webviewSource, value);
    }
}

然后像这样绑定:

<WebView x:Name="webView"  WidthRequest="1000" HeightRequest="1000" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Source="{Binding WebviewSource}">

想改的时候,试试这个:

if (WebviewSource == null)
{
    WebviewSource = new HtmlWebViewSource { Html = "<html><body>" + "Working Shows this HTML in the Webview" + "</body></html>" };
}
else
{
    WebviewSource = new HtmlWebViewSource { Html = "<html><body>" + "Not Working, Didnt update with this new HTML" + "</body></html>" };
}