locked
Enquiry on doing open dialog RRS feed

  • Question

  • User326503 posted

    I am going to include open dialog in my developed xamarin applications. I had added these code in main.cs.

    I get the code from https://developer.xamarin.com/guides/mac/user-interface/working-with-dialogs/#TheOpenDialog

    var dlg = NSOpenPanel.OpenPanel; dlg.CanChooseFiles = true; dlg.CanChooseDirectories = false; dlg.AllowedFileTypes = new string[] { "txt", "html", "md", "css" };

            if (dlg.RunModal() == 1)
            {
                // Nab the first file
                var url = dlg.Urls[0];
    
                if (url != null)
                {
                    var path = url.Path;
    
                    // Create a new window to hold the text
    
                    var newWindowController = new WindowController();
                    newWindowController.Window.MakeKeyAndOrderFront(this);
    
                    // Load the text into the window
                    var window = newWindowController.Window as MainWindow;
                    window.Text = File.ReadAllText(path);
                    window.SetTitleWithRepresentedFilename(Path.GetFileName(path));
                    window.RepresentedUrl = url;
    
                }
            }
    

    But the error shown

    Friday, May 26, 2017 10:02 AM

All replies

  • User125801 posted

    This code should go in AppDelegate.cs not Main.cs. Take a look at the sample code here for an example:

    https://github.com/xamarin/mac-samples/blob/c81c0ba9a0a85b43201d6a3332b5ff26a6570410/MacWindows/MacWindows/AppDelegate.cs#L49-L78

    Friday, May 26, 2017 11:26 AM