我创建了一个新的Xamarin Forms Prism项目来复制我在实际应用中遇到的这个问题。我想转到我的数据/网络服务并获取我的应用程序首页的公告。它只是一个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请告诉我。我一直在看这个2天,仍然不能像在Android上那样显示它
答案 0 :(得分:2)
如果要更改WebView的内容,请尝试绑定其HtmlWebViewSource
而不是Html
。
在您的viewmodel中创建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>" };
}
答案 1 :(得分:0)
老实说,我真的不明白你要对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
另外,我认为最新版本的Prism具有基于任务的导航方法。
更新
您更新了问题。
我怀疑您的问题是您没有导航到MainPage = new MainPage()
,而是OnNavigatedTo
。因此永远不会调用Announcement
,并且永远不会设置public class Class3 {
public static int[] insert(int[]x,int item,int position){
int[] newX= new int[x.length+1];
position =Math.min(x.length, position);
for (int i=0;i<position;i++){
newX[i]=x[i];
}
newX[position]=item;
for (int indexinOld)=position.indexinOld<x.length;indexinOld++){
newX[indexinOld+1]=x[indexinOld]
}
。