共用方式為


Globals 介面

更新:2007 年 11 月

Globals 物件是在 Visual Studio 環境的各個工作階段期間,以及使用 VariablePersists 屬性的跨工作階段期間,用來儲存資料的快取。

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

語法

<GuidAttribute("E68A3E0E-B435-4DDE-86B7-F5ADEFC19DF2")> _
Public Interface Globals

Dim instance As Globals
[GuidAttribute("E68A3E0E-B435-4DDE-86B7-F5ADEFC19DF2")]
public interface Globals
[GuidAttribute(L"E68A3E0E-B435-4DDE-86B7-F5ADEFC19DF2")]
public interface class Globals
public interface Globals

備註

例如,Globals 物件可以讓程式具有在執行間維持數值不變的全域變數。如果每次執行時都會要求使用者輸入資訊,也可以利用此物件讓命令能夠實作預設值。此外,它可用來在叫用特定次數後變更其行為。

資料在 Globals 物件中是儲存為名稱/變數值組。透過使用 VariablePersists 屬性,可以選擇性地將這些名稱/變數值組儲存在磁碟中,並且維持它們在不同 Visual Studio 工作階段間的狀態 (以字串的形式)。

注意事項:

但是包含物件或 SafeArrays 的變數無法儲存。如果值可以儲存為字串的話,它將會以其原生 (Native) 格式儲存。

增益集或巨集也可使用 Globals 物件,儲存對 Visual Studio 工作階段間各使用者都不相同的使用者自訂資料。它們也可以使用 Globals 物件對方案 (.sln) 檔儲存或擷取資料。

使用 VariableValue 屬性來儲存或讀取隨同 Globals 物件儲存的值。

注意事項:

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 Globalsglobals = 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.");
}

請參閱

參考

Globals 成員

EnvDTE 命名空間

其他資源

保存專案與方案中的資訊