繁体   English   中英

Visual Studio WPF 设计器从哪个目录使用?

[英]Which directory does the Visual Studio WPF Designer use from?

在我的简单 WPF 应用程序中,我试图从数据库中获取数据。 当我构建并运行应用程序时,它会正确地从数据库中读取数据,但在设计器中出现错误

SQLite 错误 14:“无法打开数据库文件”。

由于发生这种情况,DataContext 以某种方式“中断”并且来自该文件的 DataBinding 的 rest 不会“绑定”,这会扰乱设计的 rest。

这是访问数据库的代码。

public static string ReturnNoteTitle()
{
    using var connection = new SqliteConnection("Data Source=notesDB.db");
    //var connection = ConnectDB();
    connection.Open();            
    var command = connection.CreateCommand();
    command.CommandText = @"SELECT noteContents FROM userNotes WHERE noteUsername = 'xx'";
    var reader = command.ExecuteReader();
    string title = "";
    while (reader.Read())
    {
        title = reader.GetString(0);
    }
    connection.Close();           
    
    return title;
}

有任何想法吗?

设计者将 output 复制到另一个文件夹中,这样它就不会被锁定并防止重新编译。 但是它不会在那里复制其他文件,因此您需要项目的绝对路径并将其添加到前面。 你可以得到

public static string GetProjectLocation(
#if DEBUG   // use this compiler switch to prevent shipping internal paths to our customers
   [CallerFilePath]
#endif
    string sourceFilePath = "")
{
    if (string.IsNullOrEmpty(sourceFilePath))
       throw new InvalidOperationException("Use DEBUG mode for XAML deginer!");
    return Path.GetDirectoryName(sourceFilePath);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM