Reputation: 81
I have created a new Xamarin Forms Prism project to duplicate this issue I am having with my actual app. I want to go to my data/web service and fetch announcements for the front page of my app. It is just an HTML string so I am using a WebView and my MainPage.xaml looks like .
<?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>
And my 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();
}
}
}
In the OnNavgatedTo function it does not update the HTML that is displayed when you Navigate away from this page and then return.
If anyone out there knows why this might be working fine on android but not iOS please let me know. I have been looking at this for 2 days now and still can not get it to display like it does on Android
Upvotes: 0
Views: 1122
Reputation: 6641
If you want to change the WebView's content, try to bind its HtmlWebViewSource
instead of Html
.
Create your HtmlWebViewSource
property in your viewmodel:
private HtmlWebViewSource _webviewSource;
public HtmlWebViewSource WebviewSource
{
get { return _webviewSource; }
set
{
SetProperty(ref _webviewSource, value);
}
}
Then bind it like:
<WebView x:Name="webView" WidthRequest="1000" HeightRequest="1000" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Source="{Binding WebviewSource}">
When you want to change it, try this:
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>" };
}
Upvotes: 2
Reputation: 3274
Honestly I don't really understand what you're trying to do with the Task.Run
.
Just do:
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}");
}
Also I think newest versions of Prism have Task based navigation methods.
Update:
You updated your question.
I suspect your problem is that you don't navigate to your MainPage
but do MainPage = new MainPage()
. Hence OnNavigatedTo
is never called and Announcement
is never set.
Upvotes: 0