【问题标题】:The name 'Assets' does not exist in the current context当前上下文中不存在名称“资产”
【发布时间】:2013-04-11 14:21:46
【问题描述】:

我对这行代码有疑问。想在设备上创建 sqlite 数据库。

 string dbPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\test.db";
        if (!System.IO.File.Exists(dbPath))

            using (System.IO.Stream sr = ***Assets***.Open("test.db"))
            {
                using (System.IO.Stream srTo = System.IO.File.Create(dbPath))
                {
                    sr.CopyTo(srTo);
                }
            }

此消息提供: 当前上下文中不存在名称“资产”

有一个类似的项目,但比大的更全面。没有错误。他们在我遵循定义的地方定义了 c 驱动器中的 cs 文件,提供了对 Android.Content.ContextWrapper 的引用。 不在项目路径上。 [文件如何被添加应用程序?

【问题讨论】:

  • 你的代码在哪里?这是在活动中吗?
  • 是的,它在 Activity 中

标签: android xamarin.android assets


【解决方案1】:

如果您不在活动中执行此操作,则需要对活动/上下文的引用。您需要将其传递给构造函数中的帮助程序类。

public yourClass(Activity context.......){

      context.Assets.Open("your.db");
}

【讨论】:

  • 慈悲。您已经完成了更改,但仍然是同样的问题。
  • 我刚刚在我的机器上运行了一个示例。只要您持有活动参考,资产就应该在那里。它应该编译。 Assets 是 AssetManager 的 C# 属性,它仅适用于 Activity 而不是全局的。
  • 有一个类似的项目,但比 big 更全面。没有错误。他们在我遵循定义的地方定义了 c 驱动器中的 cs 文件,提供了对 Android.Content.ContextWrapper 的引用。不在项目路径上。 [文件是如何被添加应用的?
【解决方案2】:

你可以这样做:

using (System.IO.Stream sr = Android.App.Application.Context.Assets.Open("test.db"))
{
     using (System.IO.Stream srTo = System.IO.File.Create(dbPath))
     {
          sr.CopyTo(srTo);
     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2014-04-22
    • 2017-06-17
    • 2015-09-11
    • 1970-01-01
    相关资源
    最近更新 更多