【问题标题】:how can I open index.html in main activity in Xamarin forms app cross platform application?如何在 Xamarin 表单应用程序跨平台应用程序的主要活动中打开 index.html?
【发布时间】:2019-12-22 15:45:15
【问题描述】:

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

【问题讨论】:

标签: forms xamarin


【解决方案1】:

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

首先,在Android--Assets文件中创建html,命名为TestWebPage.html。

然后在Form,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

【讨论】:

  • 谢谢 Cherry 的帮助 :) 我需要一点帮助,我该如何联系你?
  • @Mohammad Ahmad Al Ajouri,你能描述一下你遇到了什么问题,我会看到然后尽可能帮助你。
  • 移动应用程序 (Xamarin.Forms) 我在 Visual Studio 2019 移动应用程序 (Xamarin.Forms) 跨平台创建项目,我需要什么 .. 当用户启动应用程序(android 和 IOS ) 启动我的 index.html 并在我的 html 文件之间进行渲染。想要查看的文件已经包含在名为 HTML 文件的项目中,请问项目中的这个文件可以吗?附件找到项目和html文件drive.google.com/drive/folders/…
  • @Mohammad Ahmad Al Ajouri,你试试我的样品吗?我下载了你的项目,但发现是空白项目,请先下载我的示例进行测试:github.com/CherryBu/htmlapp
  • 你能在你的项目中添加我的 www 文件吗?当开始你的项目时,我的 index.html 的主要活动是可能的:) ?
猜你喜欢
  • 2017-12-25
  • 2018-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
相关资源
最近更新 更多