I have a UWP app where the view contains a WebView
.
In my ViewModel, I have two variables, one that contains a URI and another that contains HTML content.
I need to be able to navigate to the URI OR display the HTML content, depending on the state of a ToggleButton.
I can bind the Source
property of the WebView
to navigate to a URI but I don't know how to load the HTML content in the WebView
.
I know that there is a method WebView.NavigateToString(string)
but don't know how to call this from my ViewModel. I've read that you should call this from the code behind of the view but my view can't see the content that's in my ViewModel.
The obvious way would be to get a reference to the WebView
from my ViewModel but I don't know how to do this and it spoils the separation of the MVVM pattern.
Can anyone suggest a solution?
public class MainPageViewModel : INotifyPropertyChanged
{
// These properties all observable - notification omitted
// for brevity
public bool UseUri { get; set; }
public string HtmlContent { get; set; }
public Uri WebUri { get; set; }
}
public sealed partial class MainPage : Page
{
public MainPageViewModel ViewModel { get; }
public MainPage()
{
this.InitializeComponent();
ViewModel = new MainPageViewModel();
}
}
<Page
x:Class="ReaderZero.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<WebView Source="{x:Bind Path=ViewModel.SelectedEntry.Uri, Mode=OneWay}" />
</Page>