【问题标题】:Why am I getting "An object reference is required for the non-static field, method, or property 'Photrax.App.DBPath.get'"?为什么我会收到“非静态字段、方法或属性 'Photrax.App.DBPath.get' 需要对象引用”?
【发布时间】:2014-10-18 00:02:16
【问题描述】:

利用代码 here,我将其添加到 App.xaml.cs:

sealed partial class App : Application
{
    public string DBPath { get; set; }

    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;
    }

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {

        . . .
        this.DBPath = Path.Combine(
            Windows.Storage.ApplicationData.Current.LocalFolder.Path, "photrax.sqlite");
        . . .

...但是这段代码:

internal static List<PhotraxBaseData> SavePhotoset(List<PhotraxExifData> exifData)
{
    using (var db = new SQLite.SQLiteConnection(App.DBPath))
    . . .

...失败,“非静态字段、方法或属性 'Photrax.App.DBPath.get' 需要对象引用”

Photrax 是我项目的命名空间;但是附加的“get”爵士乐是怎么回事?

我需要做什么来为 SQLiteConnection 构造函数提供所谓的全局 db 路径?

【问题讨论】:

标签: c# sqlite windows-store-apps app.xaml


【解决方案1】:

DBPath 是App 的非静态属性。为了访问它,您需要一个App 的实例。您正尝试像访问静态属性一样访问它。

如果数据库的路径在您的应用程序中没有改变(例如,您没有多个数据库,每个数据库都由不同的 App 实例引用,您可以考虑将该属性设为静态。

【讨论】:

  • 好的;我在想“App”会是“无处不在”的东西。是的,我把它变成了静态的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多