共用方式為


Globals.VariablePersists 屬性

VariablePersists 屬性適用於數種類型的 Globals 物件。 對於 DTE.Globals 物件,它會取得或設定環境是否要保留變數,還有這個變數能否在環境的不同工作階段使用。 對於 Solution.Globals 物件,它會取得或設定環境是否要保留變數,以及這個變數能否在環境的不同工作階段和載入、卸載方案時使用。 對於 Project.Globals 物件,取得或設定環境是否要將變數保留在專案檔案中。

命名空間:  EnvDTE
組件:  EnvDTE (在 EnvDTE.dll 中)

語法

'宣告
Property VariablePersists ( _
    VariableName As String _
) As Boolean
    Get
    Set
bool this[
    string VariableName
] { get; set; }
property bool VariablePersists[String^ VariableName] {
    bool get (String^ VariableName);
    void set (String^ VariableName, bool value);
}
abstract VariablePersists : bool with get, set
JScript 不支援索引屬性。

參數

  • VariableName
    型別:System.String
    必要項。代表要保留的變數名稱。

屬性值

型別:System.Boolean
布林值,表示變數是否存在。如果變數存在,VariablePersists 會傳回 true,否則會傳回 false。

備註

雖然全域變數在 Visual Studio 的工作階段中必定維持一致,但 VariablePersists 可讓這些變數在工作階段間仍然保持不變。

注意事項注意事項

如果要使用特定方案儲存變數,請使用 DTE.Solution.Globals

如果變數不存在,VariablePersists 會傳回 false。

對於 Solution 物件 (Solution.Globals),在儲存方案時就會儲存資料。 修改 Globals 物件會使方案檔標記為已編輯 (或「記錄變更旗標 (Dirty)」)。 對於 DTE 物件 (DTE.Globals),在關閉 Visual Studio 環境或儲存方案時,就會儲存資料。 不論是哪一種情況,資料都會儲存在方案 (.sln) 檔或者 User Profiles 目錄的結構化儲存體 (Structured Storage) 檔案中。

在關閉環境或者發生 Save All 時,就會儲存所有的全域變數。 如果 VariablePersists 與 DTE 物件相關聯,數值會儲存在 Visual Studio 環境的使用者選項目錄中。

如果全域變數與 Solution 物件相關聯,值會儲存在方案 (.sln) 檔案中。 只要環境寫入 .sln 檔案的時候就會儲存這些值。

儲存的任何變數都會覆寫先前儲存的值。 若要從儲存的檔案中移除變數,請將 VariablePersists 設定為 false。 環境將會在下一次 Save 作業時移除它的值。

注意事項注意事項

VariableValue 名稱不能包含空白。 如果有名稱包含空白,會出現錯誤訊息「值未落在預期的範圍內」。

範例

Sub OnAddinLoaded(ByVal dte As DTE)
    ' Count the number of times an add-in is loaded
    ' and store the value in the solution.
    Dim globals As Globals
    globals = dte.Solution.Globals
    If globals.VariableExists("AddinLoadCounter") Then
        ' The counter has already been set, so increment it.
        Dim int32 As System.Int32
        int32 = System.Int32.Parse(CStr(globals("AddinLoadCounter")))
        int32 += 1
        globals("AddinLoadCounter") = int32.ToString()
    Else
        ' Counter has never been set, so create and initialize it.
        globals("AddinLoadCounter") = 1.ToString()
        globals.VariablePersists("AddinLoadCounter") = True
    End If
    MsgBox("This add-in has been loaded: " & _
    globals.VariableValue("AddinLoadCounter") & " times.")
End Sub
void OnAddinLoaded(_DTE applicationObject)
{
    // Count the number of times an add-in is loaded
    // and store the value in the solution.
    Globals globals;
    globals = applicationObject.Solution.Globals;
    if(globals.get_VariableExists("AddinLoadCounter"))
    {
        // The counter has already been set, so increment it.
        System.Int32 int32;
        int32 = System.Int32.Parse((string)
        globals["AddinLoadCounter"]);
        int32++;
        globals["AddinLoadCounter"] = int32.ToString();
    }
    else
    {
        // Counter has never been set, so create and initialize it.
        globals["AddinLoadCounter"] = 1.ToString();
        globals.set_VariablePersists("AddinLoadCounter", true);
    }
    System.Windows.Forms.MessageBox.Show("This add-in has been loaded: 
    " + globals.VariableValue["AddinLoadCounter"] + " times.");
}

.NET Framework 安全性

請參閱

參考

Globals 介面

EnvDTE 命名空間

其他資源

保存專案與方案中的資訊

HOW TO:編譯和執行 Automation 物件模型程式碼範例