无法将文本框文本转换为列表 - WPF

Cannot to convert text-box text to list - WPF

我正在尝试将 textBox.text 转换为列表, 它给了我一个错误说 CS0029 无法将类型 'System.Collections.Generic.List' 隐式转换为 'System.Collections.Generic.List'

主要Class:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Making_A_Language
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow: Window
    {
        string filePath = @"D:\programs\Csharp\Making_A_Language\Making_A_Language\Code\Main.fysn";
        List<string> lines = new List<string>();
        List<string> TxtLines = new List<string>();
        string text;

        public MainWindow()
        {
            InitializeComponent();
            lines = File.ReadAllLines(filePath).ToList();

        }

        private void Btn_Save_Click(object sender, RoutedEventArgs e)
        {
            text = TxtBox_Code.Text;
            TxtLines = text.ToList(); // <-- error is thrown here
            foreach (String txtLine in TxtLines)
            {

            }
        }
    }
}

Xaml:

<Window x:Class="Making_A_Language.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Making_A_Language"
        mc:Ignorable="d"
        Title="MainWindow" Height="800" Width="1500" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" Background="#FF011D40">
    <Grid>
        <TextBox x:Name="TxtBox_Code" HorizontalAlignment="Left" Height="669" TextWrapping="Wrap" Text="// Insert Code" VerticalAlignment="Top" Width="1474" Margin="10,92,0,0" BorderBrush="{x:Null}" FontFamily="Segoe UI Light" FontSize="24" Background="#FF002451" Foreground="#FF00FF17"/>
        <Button x:Name="Btn_RunCode" Content="▷" HorizontalAlignment="Left" Margin="543,10,0,0" VerticalAlignment="Top" Width="227" Height="72" BorderBrush="{x:Null}" Background="#FF002451" Foreground="#FF00FF17" FontFamily="Segoe UI Light" FontSize="36" Style="{StaticResource RoundButton}"/>
        <Label Content="Code" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="77" Width="296" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="48" FontFamily="Segoe UI Light" Foreground="#FF00FF18"/>
        <Button x:Name="Btn_Save" Content="" HorizontalAlignment="Left" Margin="311,10,0,0" VerticalAlignment="Top" Width="227" Height="72" Style="{StaticResource RoundButton}" FontFamily="Segoe UI Light" FontSize="24" Foreground="#FF00FF18" Click="Btn_Save_Click"/>
        <Button x:Name="Btn_DeleteAll" Content="️" HorizontalAlignment="Left" Margin="775,10,0,0" VerticalAlignment="Top" Width="227" Height="72" Style="{StaticResource ResourceKey=RoundButton}" FontFamily="Segoe UI Light" FontSize="24" Foreground="#FF00FF18"/>

    </Grid>
</Window>

我在 TxtLines = text.ToList(); 收到错误 XAML 中的按钮有自定义样式,如果您需要 App.xaml,请告诉我, 如果您需要更多上下文,请告诉我。 提前致谢:)

您似乎正在尝试将 TextBox 中的多行字符串转换为字符串列表。 你在做什么不起作用,因为 ToList() 方法作为 String class 的扩展会给你字符串中的字符。在这种情况下,字符串实际上是一个 IEnumerable。请参阅以下代码的输出。

public static void Main()
    {
        string x="abcd";
        List <char> myChars = x.ToList();
        foreach (char c in myChars)
        {
            Console.WriteLine(c);
        }

    }
/*output
a
b
c
d
*/

如果我是对的,并且您的 TextBox 中确实有换行符,那么您要做的只是使用换行符作为分隔符来拆分字符串。

string [] myStrings = textBox.Text.Split('\n');
foreach (string str in myStrings) { /*do stuff here*/}

你可以轻松使用.Split

解决方案:

TxtLines.AddRange(TxtBox_Code.Text.Replace("\r", "").Split('\n'));