如何在一个有C#源代码的Xamarin.Forms页面中显示HTML?

vsnjm48y  于 2023-02-17  发布在  C#
关注(0)|答案(4)|浏览(168)

我写这段代码是为了尝试在一个网页中显示几行HTML:
我有这个XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:Japanese;assembly=Test" 
    x:Class="Test.HelpCards" 
    x:Name="HelpCards" 
    Title="Help ▹ Cards Tab">
    <ContentPage.Content>
        <ScrollView>
            <StackLayout Spacing="10" Margin="20">
               <WebView x:Name="Browser" />
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

    public HelpCards()
    {
        InitializeComponent();
        var htmlSource = new HtmlWebViewSource();
        htmlSource.Html = @"<html><body>
        <h1>ABC</h1>
        <p>DEF</p>
        </body></html>";
        Browser.Source = htmlSource;
    }

然而,当运行代码时,我只看到一个空白页。
有人知道哪里出了问题吗?

xxls0lw8

xxls0lw81#

确保为WebView指定布局选项。

<ContentPage.Content>
    <ScrollView> <!-- ScrollView not needed as WebView has inbuilt scrolling behavior -->
        <StackLayout Spacing="10" Margin="20">
           <WebView x:Name="Browser" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
        </StackLayout>
    </ScrollView>
</ContentPage.Content>
jmo0nnb3

jmo0nnb32#

Xamarin在某个时候更新了label元素,更新包括在label元素中显示HTML的方法,通过添加TextType="Html"到元素中。

    • 示例:**
<Label TextType="Html">
    <![CDATA[
    This is <strong style="color:red">HTML</strong> text.
    ]]>
</Label>
  • (来源:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/用户界面/文本/标签#显示-html)*
von4xj4u

von4xj4u3#

webview可以单独在ContentPage.Content中。不需要将其 Package 在ScrollViewStackLayout中。此方法也不需要指定水平或垂直选项。

<ContentPage.Content>
    <WebView x:Name="Browser" />
</ContentPage.Content>
fhg3lkii

fhg3lkii4#

使用webBrowser尝试此操作

public MainPage()
    {
        InitializeComponent();
        CheckWifiOnStart();
        CheckWifiContinously();
    }

    private void CheckWifiOnStart()
    {
        var source = new HtmlWebViewSource();
        source.BaseUrl = "file://android_asset/";

        //CONTROL TO SEE IF USER IS CONNECTED 
        if (!CrossConnectivity.Current.IsConnected)
        {
            Browser.Source = "file:///android_asset/index.html";
        }
        else
        {
            Browser.Source = "ONLINE URL";
        }
    }

    private void CheckWifiContinously()
    {
        var source = new HtmlWebViewSource();
        source.BaseUrl = "file://android_asset/";

        //CONTROL TO CONNECTIVITY CHANGE
        CrossConnectivity.Current.ConnectivityChanged += (sender, args) => {

            if (!CrossConnectivity.Current.IsConnected)
            {
                DisplayAlert("ERROR", "OFFLINE MODE", "OK");
                Browser.Source = "file:///android_asset/index.html";
            }
            else
            {
                DisplayAlert("INTERNET DETECTED", "ONLINE MODE", "OK");
                Browser.Source = "ONLINE URL";
            }          
        };
    }

    public interface IBaseUrl { string GetFile(); }

相关问题