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

问题描述 投票:0回答:1

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

主要类别:

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,如果您需要更多背景信息,请告诉我。在此先感谢:)

c# wpf list textbox
1个回答
0
投票

似乎您正在尝试将TextBox中的多行字符串转换为字符串列表。您正在执行的操作不起作用,因为ToList()方法作为String类的扩展将为您提供字符串中的字符。在这种情况下,字符串实际上是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
*/

如果我是对的,并且您的文本框中确实有换行符,那么您要做的就是简单地使用换行符作为分隔符来分割字符串。

string [] myStrings = textBox.Text.Split('\n');
foreach (string str in myStrings) { /*do stuff here*/}
© www.soinside.com 2019 - 2024. All rights reserved.