2

我爲C#創建了自己的項目模板,其中包含更多的項目。C#自定義項目模板

我在其中添加了我自己的嚮導。那效果很好。

,當我嘗試把一些我的項目的自定義特性參數的補充替代字典裏,在我的嚮導庫我在我的項目獲得原始值(不更換)(它保持爲「$的connectionString $ 「)。

例如,如果我添加這段代碼在RunStarted方法:

private string _connectionString = "Lorem ipsum for example"; 
public void RunStarted(object automationObject, Dictionary<string, string>replacementsDictionary, WizardRunKind runKind, object[] customParams) 
{ 
    replacementsDictionary.Add("$connectionString$", _connectionString); 
} 

而且在我的web.config:

<connectionStrings> 
    <add name="DAL.Database.Properties.Settings.MyConnectionString" connectionString="$connectionString$" providerName="System.Data.SqlClient" /> 
</connectionStrings> 

即使在我.vstemplate文件我看到這個文件被標記爲檢查和修改參數:

<ProjectItem ReplaceParameters="true" OpenInEditor="true" TargetFileName="Web.config">Web.config</ProjectItem> 

注意:如果我把硬編碼值.vstemplate文件中像這樣例如它只能:

<CustomParameters> 
    <CustomParameter Name="$connectionString$" Value="Some dummy value" /> 
</CustomParameters> 

但是,這不是我想要的。 現在我想知道,有什麼可以成爲一個問題?

回答

1

我終於找到了解決這個問題的辦法。

要從你的類庫中傳遞你的自定義參數,你需要創建你自己的字典,並在那裏放置你的自定義數據。

然後從那裏複製數據到替換字典字典。

這就是例子,你如何可以共享充滿了希望多個項目模板之間的替換值相同的字典:

private static Dictionary<string, string> _sharedDictionary = new Dictionary<string, string>(); 

public void RunStarted(object automationObject, 
     Dictionary<string, string> replacementsDictionary, 
     WizardRunKind runKind, object[] customParams) 
    { 
     if (runKind == WizardRunKind.AsMultiProject) 
     { 
      try 
      {      
       _sharedDictionary.Add("$connectionString$", connectionString); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 
     if (_sharedDictionary != null) 
     { 
      foreach (KeyValuePair<string, string> dictItem in _sharedDictionary) 
      { 
       if (!replacementsDictionary.ContainsKey(dictItem.Key)) 
       { 
        replacementsDictionary.Add(dictItem.Key, dictItem.Value); 
       } 
      } 
     } 
    } 

因爲_sharedDictionary被標記爲靜態的所有實例將共享相同的字典和需要替換的值將在您的所有項目模板中提供。

而且,不要忘了所有的鏈接項目.vstemplate文件WizardExtension節包括。