[英]Monotouch application is getting crashed in device but works fine in simulator
[英]MonoTouch iOS application can't find files in directory on device - works on simulator
如果我在設備上運行此代碼,它將返回“ get it”:
if (Directory.Exists (NSBundle.MainBundle.BundlePath + "/ARCSDev")) {
Console.WriteLine ("got it");
} else {
Console.WriteLine ("can't find it");
}
這意味着該目錄位於主捆綁包中。
我需要在此方法調用中使用該文件:
private void generateChart (int chartNumber, string palette)
{
string filePath = NSBundle.MainBundle.BundlePath + "/ARCSDev";
Console.WriteLine("Filepath - " + filePath);
Loader loader = new Loader (filePath);
loader.LoadChart (Convert.ToString (chartNumber));
上面的代碼可以在模擬器上正常運行,但不能在設備上運行。
當設備上發生錯誤時,我得到以下堆棧跟蹤:
System.InvalidOperationException: Operation is not valid due to the current state of the object
at System.Linq.Enumerable.Max[TileIndexRecord] (IEnumerable`1 source, System.Func`2 selector) [0x00000] in <filename unknown>:0
at MyCompany.Data.Arcs.Loader.ExtractWriteableBitmap (MyCompany.Data.Arcs.Records.RGBPaletteRecord rgbPalette, Double dpi, MyCompany.Data.Arcs.Raschts.ChartIndexFile indexFile, MyCompany.Data.Arcs.Raschts.RasterChartFile chartFile) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Loader.cs:571
at MyCompany.Data.Arcs.Loader.GetHiResImage (MyCompany.Data.Arcs.Records.RGBPaletteRecord rgbPalette) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Loader.cs:362
at ARCSViewer.SecondViewController.generateChart (Int32 chartNumber, System.String palette) [0x0004e] in /Users/me/Desktop/ARCSViewer/ARCSViewer/SecondViewController.cs:118
at ARCSViewer.SecondViewController.ViewDidAppear (Boolean animated) [0x00007] in /Users/me/Desktop/ARCSViewer/ARCSViewer/SecondViewController.cs:84
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at ARCSViewer.Application.Main (System.String[] args) [0x00000] in /Users/me/Desktop/ARCSViewer/ARCSViewer/Main.cs:17
該目錄包含UNIX可執行文件和發光文件。
誰能解釋發生了什么事? 該文件肯定在正確的位置(捆綁包),因為我已經測試了檢查是否與其他我知道也存在的文件一起存在的代碼。
這可能是區分大小寫的問題,模擬器不區分大小寫,而設備區分大小寫。 檢查您的文件以查看是否以正確的大小寫(即不僅是目錄)訪問所有文件。
設備上的權限與模擬器有很大的不同(受更多限制)。 在很多地方,您由於各種原因無法訪問(例如,更改某些文件會破壞應用程序數字簽名,這將使您的應用程序無法運行)。
另外,如果您的應用程序在存儲數據的位置(和時間)方面不遵循Apple准則,則您的應用程序將被拒絕(如果您定位到應用程序商店)。
Xamarin上有一篇不錯的文章 ,介紹如何使用iOS文件系統。
我解決了這個問題,我的應用程序的幾個部分使用如下代碼:
fs = new FileStream(fileName, FileMode.Open);
我需要將FileStream()的每次出現都更改為以下內容:
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
由於我的文件在捆綁包中,不需要使用寫操作,因此將文件流限制為僅讀操作,並允許正確讀取文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.