0

在下面的代码中,我试图将数组分配给Filepaths变量m_settings,但Filepaths在 LINQ 方法之外无法识别。如何将 的内容存储在FilePaths我可以在SolveInstance方法中使用的变量中?

public void ShowSettingsGui()
{
    var dialog = new OpenFileDialog()
    {
        Multiselect = true,
        Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*"
    };
    if (dialog.ShowDialog() != DialogResult.OK) return;
    var paths = dialog.FileNames;
    var FilePaths = paths.ToDictionary(filePath => filePath, File.ReadAllText);
}

private string[] m_settings = Filepaths;  

protected override void SolveInstance(IGH_DataAccess DA)
{
    if (m_settings == null)
    {
        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
        return;
    }

    DA.SetData(0, m_settings);
}
4

2 回答 2

3

我可能读得太多了,但我认为您不需要 FilePaths,您可以直接设置 m_settings ...

private Dictionary<string, string> m_settings;  

public void ShowSettingsGui()
{
    var dialog = new OpenFileDialog()
    {
        Multiselect = true,
        Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*"
    };
    if (dialog.ShowDialog() != DialogResult.OK) return;
    var paths = dialog.FileNames;
    m_settings = paths.ToDictionary(filePath => filePath, File.ReadAllText);
}

protected override void SolveInstance(IGH_DataAccess DA)
{
    if (m_settings == null)
    {
        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
        return;
    }

    DA.SetData(0, m_settings);
}

您还需要确保在 ShowSettingsGui 之后调用 SolveInstance,否则 m_settings 将始终为 null

于 2012-08-13T01:18:33.790 回答
1
public void ShowSettingsGui()
{
    var dialog = new OpenFileDialog()
    {
        Multiselect = true,
        Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*"
    };
    if (dialog.ShowDialog() != DialogResult.OK) return;
    var paths = dialog.FileNames;
    var FilePaths = paths.ToDictionary(filePath => filePath, File.ReadAllText);

    // You need to add this
    this.m_settings  = FilePaths;
}

// You also need to change the type of m_settings from string[] to Dictionary<string, string>
private Dictionary<string, string> m_settings = Filepaths;  

protected override void SolveInstance(IGH_DataAccess DA)
{
    if (m_settings == null)
    {
        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
        return;
    }

    DA.SetData(0, m_settings);
}
于 2012-08-13T01:17:32.143 回答