"System.NullReferenceException" with GTK#

Poto_feuPoto_feu ✭✭Member ✭✭

As a beginner, I wanted to make a simple game with Xamarin.Forms using GTK#. Unfortunately, when I started to use a Navigation Page, there is a line throwing a "System.NullReferenceException" error. When I debug the app with a real Android device it runs fine, so I guess the problem lies the GTK# part.

Here's the code for the GTK# part :

public static void Main (string[] args)
{
    Gtk.Application.Init();
    Forms.Init();

    var app = new App();
    var window = new FormsWindow();
    window.LoadApplication(app); //The line throwing the error
    window.SetApplicationTitle("OpenCountrySim");
    window.Show();

    Gtk.Application.Run();
}

And here's the app constructor :

public App()
{
    InitializeComponent();
    MainPage = new NavigationPage(new MainPage());
}

Best Answer

Answers

  • LeonLuLeonLu Xamurai Member, Xamarin Team Xamurai

    Did you refer to this link step by step?
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/other/gtk?tabs=windows

    Note: You should download the GTK# for .NET, at the begining, I did not have 2.12 folder in C:\Program Files (x86)\GtkSharp path . We should repair the GTK# for .NET in the exe application.

    I test above link step by step, I can run it normally.Here is my screenshot.

    Here is my demo.

  • Poto_feuPoto_feu ✭✭ Member ✭✭

    @LeonLu Sorry if I didn't explain myself more clearly : I've already followed the steps to create the GTK# project and it works with a simple Content Page. It's when I create a Navigation Page that the error is thrown.

  • LeonLuLeonLu Xamurai Member, Xamarin Team Xamurai

    I test it in my project, I get the same error, But if I set the MainPage = new MainPage(); then If I want to achieve navigate, I used following code.

      Navigation.PushModalAsync(  new MyContentPage());
    
  • Poto_feuPoto_feu ✭✭ Member ✭✭

    Well it works now. But does it means that I can't use a Navigation Page with GTK# and that I must make a Navigation Bar substitute ?

  • Poto_feuPoto_feu ✭✭ Member ✭✭

    It does work now ! Thank you for your help.

Sign In or Register to comment.