[英]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.