iOS应用中使用Xamarin的FileHelper实现

时间:2017-02-15 16:59:36

标签: sqlite xamarin xamarin.ios xamarin.forms

我最近跟随Xamarin的SQLite tutorial安装了SQLite-net PCL。在Debug模式下,一切都在模拟器上完美运行,但我在Release模式下启动时崩溃了。

例外情况如下:

异常:

  

system.missingMethodException而

消息:

  

找不到类型MyApp.iOS.FileHelper

的默认构造函数

堆栈跟踪:

  

at(包装器托管到原生)UIKit.UIApplication:UIApplicationMain   UIKit.UIApplication.Main上的(int,string [],intptr,intptr)   (System.String [] args,System.IntPtr principal,System.IntPtr   代表)[0x00005] in   /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/src/UIKit/UIApplication.cs:79   在UIKit.UIApplication.Main(System.String [] args,System.String   principalClassName,System.String delegateClassName)[0x00038] in   /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/src/UIKit/UIApplication.cs:63   在MyApp.iOS.Application.Main(System.String [] args)[0x00008]中   /用户/ {文件路径} /iOS/Main.cs:13

我发现了什么

因此,此MyApp.iOS.FileHelperXamarin's code,用于获取文档目录。实现如下:

表单应用程序中,我们只签订了合同:

public interface IFileHelper
{
    string GetLocalFilePath(string filename);
}

MyApp.iOS项目中,我们定义了一个依赖项:

[assembly: Dependency(typeof(FileHelper))]
namespace MyApp.iOS
{
    public class FileHelper : IFileHelper
    {  
        public FileHelper() { }

        public string GetLocalFilePath(string filename)
        {
             string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string libFolder = Path.Combine(docFolder, "..", "Library", "Databases");

            if (!Directory.Exists(libFolder))
            {
                Directory.CreateDirectory(libFolder);
            }

            return Path.Combine(libFolder, filename); 
        }
    }
}

以下是我使用它的方式:

DependencyService.Get<IFileHelper>().GetLocalFilePath("MyAppDatabase.db3")

应用程序在调试模式下按预期工作。因此,Dependency Service在Release中的行为方式不同,或者Document中的Documents Directory不同。

我的问题

如何在发布模式下避免此异常?

依赖关系信息:

Xamarin表格2.3.3.193 Sqlite-net-pcl 1.2.1

更新

我尝试了什么:

  • 添加了默认构造函数,不产生任何更改
  • 尝试了不同的链接器可能性,不会产生任何变化

2 个答案:

答案 0 :(得分:0)

来自Introduction to DependencyService

  

请注意,每个实现都必须具有默认值(无参数)   构造函数,以便DependencyService能够实例化   它。无参数构造函数不能由接口定义。

您需要在FileHelper类中添加一个空构造函数。

#is.factor(xx)

答案 1 :(得分:0)

  

请注意:我尝试投票以重复该问题的方式结束该问题。很明显   未能如此关闭。

基于从this post指向SushiHangover的评论,这解决了我的问题:

我需要在iOS项目中添加装饰我的FileHelper类的内容:

[Preserve(AllMembers = true)]

再次,请在此处查看他的原始帖子: https://stackoverflow.com/a/41932246/1664443

相关问题