繁体   English   中英

Xamarin表单在Android上显示Binded Webview,但在等待的任务上不在IOS上显示

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

我创建了一个新的Xamarin Forms Prism项目,以复制我在实际应用中遇到的此问题。 我想转到我的数据/ 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函数中,当您离开此页面然后返回时,它不会更新显示的HTML。

如果有人知道为什么这可能在android上能正常工作,但在iOS上却不行,请告诉我。 我已经看了两天了,但仍然无法像在Android上一样显示它

如果要更改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>" };
}

老实说,我不太了解您要如何使用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}");
    }

我也认为棱镜的最新版本具有基于任务的导航方法。

更新:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM