如何在Xamarin表单应用程序跨平台应用程序的主要活动中打开index.html?

时间:2019-12-22 15:45:15

标签: forms xamarin

我使用Visual Studio 2019移动应用程序(Xamarin.Forms)创建跨平台应用程序,我想将打开主活动更改为index.html,而不更改为默认活动 我应该改变吗? 我的文件夹名称为asset / index.html

1 个答案:

答案 0 :(得分:0)

根据您的描述,您想在内容页面中加载本地html,建议您使用 DependencyService 从Android Assets文件中获取html url,我在android中创建了简单的代码,您可以看。

首先,在Android资产文件中创建html,名称为TestWebPage.html。

然后以IBaseUrl形式创建接口。

public interface IBaseUrl
{
    string GetUrl();
}

然后在Android平台中实现此接口:

[assembly: Dependency(typeof(LocalFiles))]
namespace htmlApp.Droid
{
public class LocalFiles : IBaseUrl
{
    public string GetUrl()
    {
        return "file:///android_asset/";
    }
}
}

在ContentPage中添加一个WebView。

 <StackLayout>
    <!--  Place new controls here  -->
    <WebView
        x:Name="webviewjava"
        HeightRequest="300"
        WidthRequest="300" />
</StackLayout>

 public MainPage()
    {
        InitializeComponent();

        var urlSource = new UrlWebViewSource();

        string baseUrl = DependencyService.Get<IBaseUrl>().GetUrl();
        string filePathUrl = Path.Combine(baseUrl, "TestWebPage.html");
        urlSource.Url = filePathUrl;
        webviewjava.Source = urlSource;
    }

这是我在github上的示例,您可以下载进行测试。

https://github.com/CherryBu/htmlapp

如果我的回复对您有帮助,请记住将我的回复标记为答案,谢谢。