【问题标题】:Cannot to convert text-box text to list - WPF无法将文本框文本转换为列表 - WPF
【发布时间】:2020-06-06 23:10:08
【问题描述】:

我正在尝试将 textBox.text 转换为列表, 它给了我一个错误,说 CS0029 无法将 type'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, 如果您需要更多上下文,请告诉我。 在此先感谢:)

【问题讨论】:

  • 你想用 Text 属性做什么。
  • @bolkay 我正在尝试将文本框中的文本写入文件
  • 如果是这样,您为什么要尝试转换为列表?只需使用文件类。 File.WriteAllText(string path, string text).
  • @bolkay 我试试
  • 我试过这个private void Btn_Save_Click(object sender, RoutedEventArgs e) { File.WriteAllLines(filePath, TxtBox_Code.Text.ToList()); //&lt;-- error here lines = File.ReadAllLines(filePath).ToList(); } 但它给了我这个错误:CS1503 Argument 2: cannot convert from 'System.Collections.Generic.List' to 'string[]

标签: c# wpf list textbox


【解决方案1】:

您可以轻松使用 .Split

解决方案:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多