Using WebView to display local page html in my crossplatform xamarin app

为君一笑 提交于 2020-02-08 10:08:52

问题


i'm new in developing app with xamarin. I'm developing my first crossplatform app using xamarin forms. If you can give me the simpliest solution for my problem with examples.

i have a MainPage.xaml with some stacklayout. in one of this stacklayout i want to display a local page.html using a Webview (i think).

thanks you

my page xaml is this

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App1.info"
             Title="WebView">
    <ContentPage.Content>
        <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <WebView x:Name="webview" Source="{Binding mySource}"  />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

my page.xaml.cs is this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App1
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class info : ContentPage


    {
        public HtmlWebViewSource mySource { get; set; }
        public info()
        {
            InitializeComponent();
            BindingContext = this;

            mySource = new HtmlWebViewSource
            {
                Html = @"<html><body>
  <h1>Xamarin.Forms</h1>
  <p>Welcome to WebView.</p>
  </body></html>"
            };
        }
    }
}

回答1:


in xaml

<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">

   <WebView x:Name="webview" Source="{Binding mySource}"  />

</StackLayout>

in code behind or ViewModel

public HtmlWebViewSource mySource { get; set; } 

public MainPage()
{
   InitializeComponent();  

   webview.Source = "@"<html><body>
  <h1>Xamarin.Forms</h1>
  <p>Welcome to WebView.</p>
  </body></html>";";
}

Or you can use data binding(MVVM) .



来源:https://stackoverflow.com/questions/58874137/using-webview-to-display-local-page-html-in-my-crossplatform-xamarin-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!