在编译时捕获资源文件名错误

发布于 2025-01-08 14:33:17 字数 485 浏览 8 评论 0原文

我正在使用 Monotouch 制作我的第一个 iOS 应用程序,并且从我的资源目录中加载了大量图像。时不时地,我会在文件名中遇到拼写错误,然后应用程序就会崩溃,并抛出一些难以理解的错误消息。 (我现在会尝试将破译堆栈跟踪添加到我的技能集中......)

我在想必须有一种更聪明的方法来处理这个问题。例如,可以有一个实用程序脚本,该脚本遍历资源目录并根据其内容构建全局常量列表。资源中的每个文件都有一个条目。

这样 MyResources/Icons/HomeIcon.png 将由常量 MyResources.Icons.HomeIcon_png 表示。然后,人们可以使用 Inotify(不知道 Mac 上会是什么)之类的东西来监视资源目录并在每次更改时重新生成常量文件。

这当然也会为资源提供很好的自动完成功能。

也许 Monodevelop 或在线某个地方已经有类似的东西了?否则我该如何设置它?

或者也许有其他一些聪明的方法来缓解这个问题?

I'm doing my first iOS App with Monotouch and I'm loading quite a lot of images from my resources directory. Every now and then I get a typo in a filename and the app will then crash on me spewing out some unintelligible error message. (I'll try adding deciphering stack traces to my skill set any day now ...)

I was thinking that there must be a smarter way to handle this. For example one could have a utility script that goes through the resources directory and constructs a list of global constants based on its contents. Each file in the resources gets an entry.

So that MyResources/Icons/HomeIcon.png will be represented by the constant MyResources.Icons.HomeIcon_png. Then one could have something like Inotify (don't know what that would be on Mac) watch the resources directory and regenerate the constants file on every change.

This would of course also give nice autocompletion for resources.

Maybe there's already something like this is already in Monodevelop or online somewhere? Otherwise how would I go about setting it up?

Or maybe there's some other smart way of mitigating the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遇见了你 2025-01-15 14:33:17

您的主要问题是资源名称中的拼写错误没有被尽早发现,并且仅在应用程序实际运行时导致崩溃。

您提出的基于可用资源生成的全局常量列表的解决方案有点简洁,但据我所知,这还不存在。

同时,您可以手动构建此全局常量列表,并创建一个单元测试来验证此列表中的所有元素是否都是有效资源(当然,通过自动循环它们 - 将资源添加到列表中不需要测试的改变)。

这样您就可以更早地发现拼写错误(当您运行单元测试时而不是当您运行应用程序时),这是您最关心的问题。此外,如果您找到/编写了您设想的脚本,那么您的应用程序代码就已经准备好了。

Your primary problem is typos in resource names are not caught early, and only cause crashes when the app is actually ran.

Your proposed solution of a list of global constants generated based on the available resources is kind of neat, but as far as I know this does not exist yet.

In the mean time, you could manually construct this list of global constants, and create a unit test that verifies all the elements in this list are valid resources (by looping through them automatically, of course - adding a resource to the list should not require a change to the test).

This way you can catch typos earlier (when you run the unit test rather than when you run the app), which is your primary concern. Additionally, if you ever find/write the script you envision, your application code is already prepared.

黑白记忆 2025-01-15 14:33:17

我为您在 Xamarin bugzilla 上提交了一个增强错误:https://bugzilla.xamarin.com/ show_bug.cgi?id=3760

I filed an enhancement bug on Xamarin bugzilla for you: https://bugzilla.xamarin.com/show_bug.cgi?id=3760

心意如水 2025-01-15 14:33:17

所以我花了四个宝贵的时间来编写这个小 python 脚本来解决我的问题。目前这是解决我的问题的最佳方案。

http://github.com/oivvio/Monodevelop-Resources-as-Constants

So I spent four precious hours to cook up this little python script that sort of solves my problem. For now it's the best solution to my problem.

http://github.com/oivvio/Monodevelop-Resources-as-Constants

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文