簡體   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