我想为已经绘制到窗体的2D对象创建一个保存/加载函数。从字符串数组解析值
type circle = { X : int; Y : int; Diameter : int; Brush : Brush}
type Square = { X : int; Y : int; Length : int; Height: int; Brush : Brush}
当我创建对象时,我把它们放到2个列表中,每个类型1个。 我最初的想法是读写这些对象的文本文件,见下图:
saveFile.Click.Add(fun _ ->
for c in listOfCircles do
myfile.WriteLine("Circle," + c.X.ToString() + "," + c.Y.ToString() + "," + c.Diameter.ToString() + "," + c.Brush.ToString())
for s in listOfSquares do
myfile.WriteLine("Square," + s.X.ToString() + "," + s.Y.ToString() + "," + s.Height.ToString() + "," + s.Length.ToString() + "," + s.Brush.ToString())
myfile.Close() // close the file
而且在文本文件,它看起来像这样
Circle,200,200,50,System.Drawing.SolidBrush
Square,50,55,45,55,System.Drawing.SolidBrush
在这里,我想读这些值,然后能够解析它们并通过添加列表中的对象并重新绘制它们来重新创建对象。
let readCircle =
System.IO.File.ReadAllLines path
|> Array.choose (fun s ->
match s.Split ',' with
| [|x; y ; z ; b ; _|] when x = "Circle" -> Some (y, z, b)
| _ -> None)
let readSquare =
System.IO.File.ReadAllLines path
|> Array.choose (fun s ->
match s.Split ',' with
| [|x; y ; z ; b ; a ; _|] when x = "Square" -> Some (y, z, b, a)
| _ -> None)
这些功能给了我
val readCircle : (string * string * string) [] = [|("200", "200", "50")|]
val readSquare : (string * string * string * string) [] = [|("50", "55", "45", "55")|]
,我现在是林不知道如何从阵列获取的值的问题。下面是多个圈子的例子。
val readCircle : (string * string * string) [] = [|("200", "200", "50"); ("200", "200","50")|]
有关如何从这里/如何解决这个问题的任何想法或意见是非常感激!问题总结:我怎么能拿从数组中的值,并把它们在例如我已经创建添加功能,见下图:
listOfCircles.Add({ X = 200; Y = 200; Diameter = 50; Brush = Brushes.Black})
我不确定我是否理解这个问题。但是,假设您需要将值数组转换为形状列表,则可以使用'values |> Seq.map createCircle |> Seq.toList'。 – Daniel
我想从数组中获取值并将它们放入listOfCircles.Add函数中,请参阅编辑后的底部。 – Bobson