Instantiate an additional window with monomac/c# - c#

I've been searching all over for the answer to this question, and while I realize it's likely very trivial, somehow the answer eludes me.
I need to show a second window (launched from clicking a menu item, if that matters). I know perfectly well how to do this with winforms, but I'm not sure what the monomac/NSWindow equivalent is. I need to do this:
MyWindowClass myWindow = new MyWindowClass();
myWindow.Show();
The best info I can find on the subject says that the following should work:
MyWindowClass myWindow = new MyWindowClass();
myWindow.MakeKeyAndOrderFront(this);
But when I try that, it tells me that MyWindowClass() needs an overload, so I look at the constructor and see that it's asking for an IntPtr. Not knowing any better, I try this:
MyWindowClass myWindow = new MyWindowClass(new IntPtr());
myWindow.MakeKeyAndOrderFront(this);
This code will run without error, yet nothing happens when I try to launch the window.
I'm sure I'm just making a silly mistake, but I just haven't been able to find anything on the subject. Any help would be greatly appreciated.
Edit: For reference, here's the relevant portion of the constructor:
public MyWindowClass (IntPtr handle) : base (handle)
{
Initialize ();
}
Additional info: I'm trying to run the code above from within AppDelegate.cs in the following method:
partial void showWindow (MonoMac.Foundation.NSObject sender){
MyWindowClass myWindow = new MyWindowClass(new IntPtr());
myWindow.MakeKeyAndOrderFront(this);
}
EVEN MORE CODE AHOY:
public partial class ViewPaths : MonoMac.AppKit.NSWindow
{
#region Constructors
public ViewPaths ()
{
Initialize();
}
// Called when created from unmanaged code
public ViewPaths (IntPtr handle) : base (handle)
{
Initialize ();
}
// Called when created directly from a XIB file
[Export ("initWithCoder:")]
public ViewPaths (NSCoder coder) : base (coder)
{
Initialize ();
}
// Shared initialization code
void Initialize ()
{
}
#endregion
}
And then the actual instantiation:
public partial class AppDelegate : NSApplicationDelegate
{
MainWindowController mainWindowController;
ViewPaths display;
public AppDelegate ()
{
}
public override void FinishedLaunching (NSObject notification)
{
mainWindowController = new MainWindowController ();
mainWindowController.Window.MakeKeyAndOrderFront (this);
}
partial void viewPaths (MonoMac.Foundation.NSObject sender){
display = new ViewPaths();
display.MakeKeyAndOrderFront(this);
}
}
}
This shows a window with no UI elements of any kind.

Just add a default constructor without the handle parameter. Make sure MyWindowClass subclasses NSWindow and it should work.
Also, you may need to keep a reference to your myWindow around - so that it does not get garbage collected.

For clarity, here's the final code that solved the problem, should anyone else ever google this:
Since I had created a new monomac window with a controller, I needed to create an instance of that controller, and show the controller's window:
MyWindowController myWindow = new MyWindowController();
myWindow.Window.MakeKeyAndOrderFront(this);
This did not need a new constructor without a handle parameter implemented - that solution was working around the problem I'd created by instantiating the incorrect thing.

Related

C# Constructor on type not found when loading Design [duplicate]

I have a Visual Studio 2010 Windows Forms app which includes a Form base class that other classes will inherit. The base class' constructor takes a parameter that the child classes will pass to the base class.
Example:
public partial class BaseForm : Form
{
public BaseForm(int number)
{
InitializeComponent();
}
}
public partial class ChildForm : BaseForm
{
public ChildForm(int number)
: base(number)
{
InitializeComponent();
}
}
The problem that I'm running into is, when I attempt to open the ChildForm in VisualStudio's Design View mode, I receive the following error:
Constructor on type 'MyProject.BaseForm' not found.
Note: regardless of the error, the project compiles and runs fine.
I can avoid the error if I overload the constructor with one that does not contain any parameters.
Example: (This gets rid of the error)
public partial class BaseForm : Form
{
public BaseForm(int number)
{
InitializeComponent();
}
public BaseForm()
{
InitializeComponent();
}
}
public partial class ChildForm : BaseForm
{
public ChildForm(int number)
: base(number)
{
InitializeComponent();
}
}
My question is, how can I create a base class that does not include a parameterless constructor and avoid the Design View error?
That is completely impossible.
The form you see in the design view is an actual instance of your base class.
If there is not default constructor, the designer cannot create that instance.
You can mark the constructor with the [Obsolete("Designer only", true)], and make it throw an exception if called when not in the designer, to prevent other people from calling it.
You need to adjust your BaseForm output type, In the properties for the project, change the Output type from Windows Application to Class Library.
ref:
https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/walkthrough-demonstrating-visual-inheritance

Passing Control Object to base constructor

I want to create a parent class who handles setting up all the control properties for a map, because I need that functionality in multiple forms. Since you can not pass anything besides parameters to your child function to the parent class, I have no idea how to continue. Setting up an extra method where you have to add the control to the parent class feels kind of unclean because I need to check the reference each time I call it.
Maybe you have some ideas how to implement this problem. Thanks!
public abstract class MapForm
{
protected MapForm(GMapControl mapControl)
{
MapControl = mapControl;
}
...
}
public class TestForm : MapForm
{
public TestForm(string searchValue = String.Empty) :
Base(this.mapControl) // cant do that of course
{}
...
}

Accessing object created in different Window in WPF application

I have created one window and declared 2 instances of my object, then I modified them and wanted to pass to another Window. My questions are:
How would i do that ?
(I can pass simple types such as string or int trough window constructor but passing my own object giving me an error (Inconsistent Accessibility parameter order is less accessible then method))
Does it have any connection with dataContext ?
Can anybody explain to me how I can achieve that (in the simplest possible way)? What are the correct ways to do that ?
Here is part of my code (everything is in one namespace):
public partial class Main_window : Window
{
Order myOrder = new Order();
Menu menu = new Menu();
public Main_window()
{ InitializeComponent() }
private void OpenSecondWindow(object sender, RoutedEventArgs e)
{
Second_Window SecondWindow = new Second_Window();
Second.ShowDialog();
}
}
// Second Window class
public partial class Second_Window : Window
{
public Second_Window(Order someOrder)
{ InitializeComponent(); }
}
Make sure that the Order type, and any other type you intend to inject the SecondWindow with, is defined as a public class:
public class Order { ... }
A non-public type cannot be part of the signature of a public method or constructor.

Xamarin subclassed UINavigationController with custom UINavigationBar

I'm trying to create a non-standard top navigation bar for use throughout my application. To achieve this I've been trying to subclass UINavigationController and UINavigationBar
I have a custom NavigationController class
partial class ZooNavigationController : UINavigationController
{
public ZooNavigationController (IntPtr handle) : base (typeof(TopNavBar), null)
{
this.Handle = handle;
}
}
which points to the base constructor
public UINavigationController (Type navigationBarType, Type toolbarType);
for my custom UINavigationBar class TopNavBar which is something like...
public class TopNavBar : UINavigationBar
{
public TopNavBar ()
{
InitCustom ();
}
public void InitCustom(){
this.BackgroundColor = UIColor.Red;
// a bunch more custom stuff
}
}
The problem is, TopNavBar is never called when I run this. If I try to adjust my constructor to look like this:
public ZooNavigationController (IntPtr handle) : base (typeof(TopNavBar), null)
{
this.Handle = handle;
TopNavBar test = (TopNavBar)this.NavigationBar;
}
I get a runtime exception that it can't cast the types, so it seems that it's ignoring my call specifying the UINavigationBar type.
Can anyone help me out with what I'm missing here?
EDIT in the end it turns out I was missing the fact that you can set a custom UINavigationBar inside the storyboard. Combining that with the miguel's answer I ended up with the class
partial class TopNavBar : UINavigationBar
{
public TopNavBar(IntPtr test) : base(test) {
}
[Export ("initWithCoder:")]
public TopNavBar (NSCoder coder) : base (coder) {
InitCustom ();
}
}
The IntPtr constructor is called in response to the object being created by Objective-C and surfaced to C#. This in general is not the way that these classes are instantiated.
The first question that you have to ask yourself is: who is creating the instance of your class?
You create the instance of this class from C#: you call a constructor with the proper parameters.
Being created during deserialization (for example, loading from a storyboard, XIB, or your own archived data), then you need to provide the constructor that takes an NSCoder parameter.
Having your instance recreated on demand (may indicate a problem, because it means that your object was destroyed, but Objective-C kept a reference to it, and now it is being resurfaced again), the IntPtr constructor.
In your example above, there is a mistake: you are overriding the IntPtr constructor, which should only ever call into the base class IntPtr constructor (since it means "I have a pointer to the real object in objective-c, create a wrapper for it").
My guess is that you are using C#, so in that case, what you want is to provide a new constructor that takes no arguments:
public ZooNavigationController () : base (typeof (YourNavigation), typeof(YourBar)) {
// Your own initialization goes here
}

How to use WPF elements from another class?

I'm currently developing a chat client/server with a WPF visual interface. One of the main function in the MainWindow class is designed to write a message in a richbox in the related WPF.
public partial class MainWindow : Window
{
// VU Window
public static MainWindow vuClient;
// VU Initialization
public MainWindow()
{
InitializeComponent();
vuClient = this;
}
static public void writeChat(User pUser, String pMessage)
{
...
// Writing in the richbox
vuClient.vuChatBox.Document.Blocks.Add(formatedMessage);
}
}
The problem is that if I call writeChat() in the MainWindow() function or through an event (onclick for example) everything works as attended, but if I try to call this function via an other class nothing happens! I don't even have an error. Do you have any clue about that issue?
No need to maintain the object "vuClient". Because writeChat is a static method.
You can directly called like below
MainWindow.writeChat();
you can't use an UI element or a function in some other class. you need to make a Delegate with an event. with the help of this event you can update your UI accordingly.

Categories

Resources