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.
异常。
知道为什么会发生这种情况吗?
编辑:这是跟踪。我必须删除上面示例中的捕获才能获取转储。
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><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ba5afcefdeee5ffa5e3cbbabcbfa6baa5c2e5fde4e0ee" rel="noreferrer noopener nofollow">[email protected]</a>(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
最佳答案
查看FSCharting source code ,订阅时似乎没有事件的同步上下文。
您应该在窗口初始化时创建图表,这将在创建 WPF 同步上下文的 Application.Run
之后发生。
关于charts - 对于此 Fsharp Charting Livechart 示例,为什么未设置引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38510278/