如何在Xamarin ios中定位目录资源

Mus*_*rka 3 xamarin.ios ios xamarin xamarin-studio xamarin.forms

我目前正在使用 Xamarin.iOS 并尝试从项目资源和项目目录中的目录中获取数据。但我在获取信息时无法获取数据。我什至可以看到整个目录没有复制到应用程序包本身中。

下面是我的代码。

string localHtmlUrl = Path.Combine(NSBundle.MainBundle.BundlePath,
                                   string.Format("NewFolder/webref/index.html")
                      );
Run Code Online (Sandbox Code Playgroud)

Iai*_*ith 5

从这个示例中,我认为您所需要的只是:

string fileName = "NewFolder/webref/index.html"; // remember case-sensitive
string localHtmlUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
webView.LoadRequest(new NSUrlRequest(new NSUrl(localHtmlUrl, false)));
Run Code Online (Sandbox Code Playgroud)

如果您有这样的文件夹结构:

在此输入图像描述

如果它在您的Resource文件夹中,那么您需要将其放入您的路径中,如下所示:

string fileName = "Resource/NewFolder/webref/index.html";
Run Code Online (Sandbox Code Playgroud)

请记住将所有文件设置Build ActionBundleResource

也可以试试这个:

var fileName = "NewFolder/webref/index.html";
webView.LoadHtmlString (File.ReadAllText (fileName), NSUrl.FromFilename (fileName));
Run Code Online (Sandbox Code Playgroud)