如何将代码模块(.cs)显示为列表框中的项目?

How to display code modules(.cs) as items in a listbox?

我目前正在开发一个新的 GUI,其中有一个 listbox 作为其中的关键元素。到目前为止,我已经设法在此列表框中显示 select 多个项目 ,这没什么大不了的。

我的目标是在项目的文件夹中有几个.cs文件(可能进一步扩展为vb脚本文件),它们在主视图列表框中正确显示,如果列表框中的相应项目被 selected 将被执行。

到目前为止,我已经尝试构建列表框和所有其他 GUI 内容(按钮、文本...)并将列表框与脚本模型的可绑定集合(这是一个自己的 class 目前用于测试目的,应替换为正确的 .cs 文件)

在下面的代码中,您可以看到使用此自定义 class 和 selection 检查多个列表框项目的变通方法。

private void Run_Click(object sender, RoutedEventArgs e)
        {
            //Show user the scripts which are being processed, these are the previous selected Scripts
            List<string> selectedList = new List<string>();
            foreach (ScriptModel selectedScript in MainListBox.SelectedItems)
            {
                selectedList.Add(selectedScript.Name.ToString());
            }
            //check if no Script was selected, and if so, just return/do nothing
            if (selectedList.Count() == 0) { return; }
            MessageBox.Show("The following Scripts will be processed: " + Environment.NewLine +
                    string.Join(Environment.NewLine, selectedList));


           //Call the Connection for Data-Export 

        }
private BindableCollection<ScriptModel> _scriptscollection=new BindableCollection<ScriptModel>();

public BindableCollection<ScriptModel> ScriptsCollection
        {
            get { return _scriptscollection; }
            set { _scriptscollection = value; }
        }

我想知道如何用项目文件夹中的实际 .cs 文件(它们是某种脚本)替换(或连接)这些自定义 class,所以我可以显示这些文件名和 select 相应的文件以供执行。 (所以连接应该双向工作)

如果这个问题看起来有点奇怪和笼统,我很抱歉,但我真的很困惑。

我相信你把事情复杂化了。这是将在目录中找到所有 .cs 文件的代码,然后在 ListBox 中选择一个文件后将启动该文件。

很难准确地说出您的要求,但希望这对您有所帮助。

XAML

<ListBox ItemsSource="{Binding ScriptFiles}" SelectedItem="{Binding SelectedScript}"/>

隐藏代码/ViewModel

public List<string> ScriptFiles => Directory.GetFiles(FilePath, "*.cs").ToList();

private string selectedScript;
public string SelectedScript
{
    get { return selectedScript; }
    set { selectedScript = value; Process.Start(value); }
}