2 回答

TA貢獻1998條經驗 獲得超6個贊
我用 try/catch 塊包圍了 GetCustomUI() 方法重寫,并將異常記錄到文本文件中。這使我能夠訪問插件啟動時引發的異常。
而且,最重要的是,問題是我有一個額外的 JSON 配置文件,打包的 XLL 沒有考慮到該文件,似乎沒有直接的方法可以通過 DNA 文件包含它。
將外部文件設置為嵌入資源并從清單資源流中讀取它。
在我的特定情況下,我將它用于 DI 服務提供商,并按如下方式構建它:
private IServiceProvider BuildServiceProvider()
? ? {
? ? ? ? var serviceCollection = new ServiceCollection();
? ? ? ? //Configuration
? ? ? ? ConfigurationBuilder builder = new ConfigurationBuilder();
? ? ? ? builder.SetBasePath(Directory.GetCurrentDirectory());
? ? ? ? var assembly = Assembly.GetExecutingAssembly();
? ? ? ? var resourceName = "otherconfig.json";
? ? ? ? using (Stream stream = assembly.GetManifestResourceStream(resourceName))
? ? ? ? using (StreamReader reader = new StreamReader(stream)) {
? ? ? ? ? ? string result = reader.ReadToEnd();
? ? ? ? ? ? string tempPath = Path.GetTempFileName();
? ? ? ? ? ? File.WriteAllText(tempPath, result);
? ? ? ? ? ? builder.AddJsonFile(tempPath);
? ? ? ? }
? ? ? ? IConfiguration config = builder.Build();
? ? ? ? serviceCollection.AddSingleton(config);
? ? ? ? //other dependency injection service registration
? ? ? ? return serviceCollection.BuildServiceProvider();
? ? }

TA貢獻1847條經驗 獲得超7個贊
如果您要重寫該方法GetCustomUI,請添加try...catchon GetCustomUI,然后查看異常詳細信息。
[ComVisible(true)]
public class RibbonController : ExcelRibbon
{
public override string GetCustomUI(string RibbonID)
{
try
{
// ...
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
// ...
}
- 2 回答
- 0 關注
- 275 瀏覽
添加回答
舉報