简体   繁体   English

来自类库和xamarin.android的资源

[英]Resource from class library and xamarin.android

Update: I was since able to a text file resource to the project, whose content is in json format. 更新:从那时起,我就能够为项目提供文本文件资源,其内容为json格式。 This seems to work without issue. 这似乎没有问题。 Is this conventional though? 这是常规的吗? I guess the question still stands. 我想这个问题仍然存在。

I have a class library I wrote and tested that uses a json file. 我有一个我编写并测试过的使用json文件的类库。 Originally I had this as simply a file accessible to the project. 最初,我只是将其作为项目可访问的文件。 I can deserialize it, use it, no problem. 我可以反序列化它,使用它,没问题。

I've since added a xamarid.android project to my solution, developed my interface, wired things up, all is good. 从那以后,我在解决方案中添加了一个xamarid.android项目,开发了我的界面,将其连接起来,一切都很好。 I also referenced the earlier class library I wrote and intend to use in the android app. 我还引用了我编写的较早的类库,并打算在android应用程序中使用。

Once I execute the app and kicked off the new class though, Visual Studio stopped and complained it couldn't read the json file in question. 一旦我执行了该应用程序并启动了新类,Visual Studio便停止了并抱怨它无法读取相关的json文件。

It made sense since the original class library project was simply outputting the file to disk on build. 这是有道理的,因为原始的类库项目只是在构建时将文件输出到磁盘。 Android doesn't know about that location. Android不知道该位置。

Things I've already tried but do not work: 我已经尝试过但无法正常工作的事情:

Build Action: Embedded Resource/Resource/Compile Adding the file as a project resource . 构建操作:Embedded Resource / Resource / Compile将文件添加为项目资源 This didn't work as now Visual Studio thinks the file is binary and I cannot deserialize it...? 这没有用,因为现在Visual Studio认为该文件是二进制文件,我无法反序列化...?

For what it's worth, the error (rather obvious now): System.IO.DirectoryNotFoundException: Could not find a part of the path "/Data/BattingAverageFlair.json". 对于它的价值,该错误(现在非常明显):System.IO.DirectoryNotFoundException:无法找到路径“ /Data/BattingAverageFlair.json”的一部分。

public List<Flair> Flair = JsonConvert.DeserializeObject<List<Flair>>(File.ReadAllText("Data/BattingAverageFlair.json"));
public string GetFlair(double averge) => Flair.First(m => averge <= m.Max && averge >= m.Min).Flairtext;

Ideally my class library: BattingAverageCalculator should be completely independent of BattingAverage, my xamarin android app. 理想情况下,我的类库:BattingAverageCalculator应该完全独立于我的xamarin android应用程序BattingAverage。

Thanks for any pointers! 感谢您的指导!

You cannot open files from the application package in code apart from some special cases like images and application configuration files. 除了某些特殊情况(例如图像和应用程序配置文件)之外,您无法使用代码打开应用程序包中的文件。

Two options here: 这里有两个选择:

  1. If the jSon file you are talking about is a config sort of file which will be used to pass parameters to the app in development time. 如果您正在谈论的jSon文件是配置文件,它将在开发时用于将参数传递给应用程序。 Use Settings in App.Config for that. 为此,请使用App.Config中的“设置”。

  2. If you are using as a local storage or a client application database of some sort you should create the file in runtime (maybe with some hard-coded predefined values) in the Android personal library folder where you can access it later in code. 如果您用作本地存储或某种形式的客户端应用程序数据库,则应在运行时(可能带有一些硬编码的预定义值)在Android个人库文件夹中创建文件,以供以后在代码中访问。

You can read and write files into local storage in this folder path: 您可以在以下文件夹路径中将文件读写到本地存储中:

Environment.GetFolderPath(Environment.SpecialFolder.Personal)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM