【发布时间】: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()); //<-- error here lines = File.ReadAllLines(filePath).ToList(); }但它给了我这个错误:CS1503 Argument 2: cannot convert from 'System.Collections.Generic.List' to 'string[]