使用 WebView 在我的跨平台 xamarin 应用程序中显示本地页面 html [英] Using WebView to display local page html in my crossplatform xamarin app

查看:26
本文介绍了使用 WebView 在我的跨平台 xamarin 应用程序中显示本地页面 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用 xamarin 开发应用程序的新手.我正在使用 xamarin 表单开发我的第一个跨平台应用程序.如果您可以通过示例为我的问题提供最简单的解决方案.

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.

我有一个带有一些堆栈布局的 MainPage.xaml.在其中一个 stacklayout 中,我想使用 Webview 显示本地 page.html(我认为).

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).

谢谢

我的页面 xaml 是这个

<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>

我的 page.xaml.cs 是这个

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>"
            };
        }
    }
}

推荐答案

in xaml

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

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

</StackLayout>

在代码隐藏或 ViewModel 中

public HtmlWebViewSource mySource { get; set; } 

public MainPage()
{
   InitializeComponent();  
       
   webview.Source = "@"<html><body>
  <h1>Xamarin.Forms</h1>
  <p>Welcome to WebView.</p>
  </body></html>";";
}

或者你可以使用数据绑定(MVVM).

Or you can use data binding(MVVM) .

这篇关于使用 WebView 在我的跨平台 xamarin 应用程序中显示本地页面 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆