3

I have a problem with making the mainwindow on my simple app run, The error given is that - Object reference not set to an instance of an object.

this happens when the app is getting debugged and the error occurs at handler.window1.ShowAll()

I did find some code online which hints at adding some member code as in member this.Whatever() = window1 however i have no idea if this is relevent to my code, or where to put it.

i am happy for any help you can give me as i have been trying all day to get this working in many ways and simply cannot.

namespace potato
module Main =

    open System
    open Gtk

    type Handler()=class
        [<Object>]
        [<DefaultValue(true)>]
        val mutable window1 : Window
    end

        [<EntryPoint>]
        let Main(args) = 
            Application.Init()

            let builder =  new Builder("GUI.ui")
            let handler = new Handler()
            builder.Autoconnect(handler)

            handler.window1.ShowAll()
            Application.Run()
            0

Here is the glade.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.18"/>
  <object class="GtkWindow" id="window1">
    <property name="width_request">1024</property>
    <property name="height_request">576</property>
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
  </object>
</interface>
4

1 回答 1

3

是的,问题就在我眼前,我最终不得不通过旧的测试项目来查看并意识到@scrwtp 所暗示的内容,这是为 gtk3 gtkbuilder 修复的旧工作代码。

namespace potato
module Main =

open System
open Gtk

type Handler()=class
    [<Builder.Object>]
    [<DefaultValue(true)>]
    val mutable window1 : Window
end

let OnDelete (args:DeleteEventArgs) =
    Application.Quit()
    args.RetVal <- true 

[<EntryPoint>]
let Main (args) = 
    Application.Init()

    let gxml =  new Builder("GUI.xml")
    let handler = new Handler()
    do gxml.Autoconnect(handler)

    handler.window1.DeleteEvent
    |> Event.add OnDelete

    handler.window1.ShowAll()
    Application.Run()
    0

原因,我现在明白了,我已经指定了一个处理程序并且什么也没传递给它,因为没有任何东西传递给它 IE:(handler.window1.DeleteEvent) 当我调用 showall 时它根本不会显示,希望这可以帮助其他有类似情况的人问题

于 2017-03-02T15:24:14.313 回答