open System
open System.Windows
open FSharp.Charting
open FSharp.Charting.ChartTypes
[<STAThread>]
do
let win = Window(Title="Chart")
let data = [|for i=1 to 10 do yield (i, i*i)|]
let e = Event<_>()
let ev = e.Publish
let chart = LiveChart.Line (ev) |> ChartControl
let host = new Forms.Integration.WindowsFormsHost(Child=chart)
win.Content <- host
e.Trigger data // Throws an error here
Application().Run(win) |> ignore
我试图弄清楚F#Charting的Livecharts是如何工作的,但大多数例子都使用计时器或其他一些复杂的东西。以上是我能做到的最简单的事情,但由于某些原因,当我触发事件时,我一直得到Object reference not set to an instance of an object.
异常。
知道为什么会这样吗?
编辑:这是跟踪。我不得不删除上面例子中的catch以获得转储。
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at FSharp.Charting.NotifySeq.IObservable-1-ObserveOn@225-1.System-IObserver`1-OnNext(T value) in c:\GitHub\FSharp.Charting\src\FSharp.Charting.fs:line 226
at <StartupCode$FSharp-Core>.$Event.h@174-1.Invoke(Object sender, T args)
at Microsoft.FSharp.Control.FSharpEvent`1.Trigger(T arg)
at <StartupCode$Charts>.$Program.main@() in C:\Users\Marko\Documents\Visual Studio 2015\Projects\Poker Experiments\Charts\Program.fs:line 20
答案 0 :(得分:4)
查看FSCharting source code,似乎订阅时没有活动的同步上下文。
您应该在窗口初始化时创建图表,这将在创建WPF同步上下文的Application.Run
之后发生。