{"id":1390,"date":"2022-09-27T13:20:29","date_gmt":"2022-09-27T19:20:29","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=1390"},"modified":"2022-09-29T23:16:01","modified_gmt":"2022-09-30T05:16:01","slug":"creating-our-window-in-c","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2022\/09\/27\/creating-our-window-in-c\/","title":{"rendered":"Creating our Window in C#"},"content":{"rendered":"\n<p>So far when you run the project it will show a main window.<br>But this is because we created the kind of project we did with a main storyboard and an entry in the Info.plist that told macOS what should be the main window.<\/p>\n\n\n\n<p>It isn&#8217;t one we created in our new framework.<\/p>\n\n\n\n<p>Lets rectify that.<\/p>\n\n\n\n<p>First open Info.plist and remove the entry for NSMainStoryBoardFile. Click on the small red X icon at the right end of the entry name.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"324\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.48.24-PM-1.png?resize=525%2C324&#038;ssl=1\" alt=\"\" class=\"wp-image-1392\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.48.24-PM-1.png?resize=1024%2C632&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.48.24-PM-1.png?resize=300%2C185&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.48.24-PM-1.png?resize=768%2C474&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.48.24-PM-1.png?w=1131&amp;ssl=1 1131w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>As well remove the Main.storyboard item from the project. Right click on it and select Delete.<\/p>\n\n\n\n<p>At this point IF you run the app wont open any windows, but should still run (Check the main menu bar to verify)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"359\" height=\"245\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.51.58-PM.png?resize=359%2C245&#038;ssl=1\" alt=\"\" class=\"wp-image-1393\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.51.58-PM.png?w=359&amp;ssl=1 359w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.51.58-PM.png?resize=300%2C205&amp;ssl=1 300w\" sizes=\"auto, (max-width: 359px) 100vw, 359px\" \/><\/figure>\n\n\n\n<p>Now to create our &#8220;default window&#8221; we&#8217;ll add a class to our project<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"430\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.53.13-PM.png?resize=525%2C430&#038;ssl=1\" alt=\"\" class=\"wp-image-1394\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.53.13-PM.png?w=614&amp;ssl=1 614w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.53.13-PM.png?resize=300%2C246&amp;ssl=1 300w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"391\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.14-PM.png?resize=525%2C391&#038;ssl=1\" alt=\"\" class=\"wp-image-1395\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.14-PM.png?w=1004&amp;ssl=1 1004w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.14-PM.png?resize=300%2C224&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.14-PM.png?resize=768%2C572&amp;ssl=1 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Name this new class <em>MainWindow<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"385\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.24-PM.png?resize=525%2C385&#038;ssl=1\" alt=\"\" class=\"wp-image-1396\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.24-PM.png?w=1004&amp;ssl=1 1004w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.24-PM.png?resize=300%2C220&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.24-PM.png?resize=768%2C564&amp;ssl=1 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"307\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.38-PM.png?resize=525%2C307&#038;ssl=1\" alt=\"\" class=\"wp-image-1397\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.38-PM.png?w=772&amp;ssl=1 772w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.38-PM.png?resize=300%2C175&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-12.54.38-PM.png?resize=768%2C449&amp;ssl=1 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Notice that the IDE has no idea what superclass this new class <em>should<\/em> inherit from. Lets fix that.<\/p>\n\n\n\n<p>Recall that in C# we MUST tell the compiler what items we are going to be &#8220;using&#8221;. Since the Einhugur framework includes a definition for a Window we can tell our new class to inherit from that by adding a using clause<br>          <code>using Einhugur.Forms;<\/code><br>following the using System; that already exists and altering the class declaration to<br>       <code>public class MainWindow : Window<\/code><br><br>This style says &#8220;the class MainWindow inherits from Window&#8221;<br>And the using clause helps the compiler find the declaration of Window in the Einhugur.Forms package<\/p>\n\n\n\n<p>The other thing we can do is remove the Constructor &#8211; <code>public MainWindow()<\/code> since we dont need to do anything special in it<br><br>But how will this window get &#8220;set up&#8221; ? Note there is NOT visual designer in which we can set properties like size, placement, title etc. <\/p>\n\n\n\n<p>We&#8217;ll add a method to our new window that takes care of all this. In Xojo this also happens just the IDE writes the code based on your visual design.<\/p>\n\n\n\n<p>In this case its a method that the base Window defines that does nothing that we can override with our own so we can set things how we want.<br>Add a method to the class by just typing in between the { and } that follow the  class declaration<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/ much like the constructor in Xojo\n    protected override void SetupWindow(WindowCreationParameters prm)\n    {\n        prm.Left = 200;\n        prm.Top = 100;\n        prm.Width = 600;\n        prm.Height = 400;\n        prm.Title = \"My first window\";\n        prm.InitialPlacement = WindowPlacement.Center;\n    }<\/code><\/pre>\n\n\n\n<p>One other thing is the application doesnt know that it should show this window when it starts.<\/p>\n\n\n\n<p>Switch to the AppDelegate and we&#8217;ll add an override of the Opened method. This one is much like Xojo&#8217;s Opening event when the app starts. This gets called automatically as part of the application startup<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    protected override void Opened()\n    {\n        var window = new MainWindow();\n\n        window.Show();\n    }\n<\/code><\/pre>\n\n\n\n<p>When you run now you should get one window opened.<br>Note that there is still some work to do as we haven&#8217;t attached any menu bar. Without a menu bar the QUIT menu item doesn&#8217;t appear or function so you have to KILL the running debug app by pressing the square STOP icon<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"342\" height=\"103\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-1.16.29-PM-1.png?resize=342%2C103&#038;ssl=1\" alt=\"\" class=\"wp-image-1399\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-1.16.29-PM-1.png?w=342&amp;ssl=1 342w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-27-at-1.16.29-PM-1.png?resize=300%2C90&amp;ssl=1 300w\" sizes=\"auto, (max-width: 342px) 100vw, 342px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/great-white-software.com\/miscellaneous\/CSharp\/getting_started-2022-09-27.zip\">Here&#8217;s my app so far<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So far when you run the project it will show a main window.But this is because we created the kind of project we did with a main storyboard and an entry in the Info.plist that told macOS what should be the main window. It isn&#8217;t one we created in our new framework. Lets rectify that. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2022\/09\/27\/creating-our-window-in-c\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Creating our Window in C#&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[34],"class_list":["post-1390","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-csharp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=1390"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1390\/revisions"}],"predecessor-version":[{"id":1400,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1390\/revisions\/1400"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=1390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=1390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=1390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}