{"id":60,"date":"2006-08-30T18:56:36","date_gmt":"2006-08-30T23:56:36","guid":{"rendered":"http:\/\/www.lastdragon.net\/?p=60"},"modified":"2006-08-30T19:02:08","modified_gmt":"2006-08-31T00:02:08","slug":"programas-visuales-con-c-en-windows-y-linux","status":"publish","type":"post","link":"https:\/\/www.lastdragon.net\/?p=60","title":{"rendered":"Programas visuales con C# en Windows y Linux"},"content":{"rendered":"<p>La importancia de C# es tener la capacidad de ser portable. Esto nos ayuda a<br \/>\nescribir programas una vez y portarlos a cualquier plataforma sin<br \/>\nmodificaciones, sin embargo siempre hay un pero.<\/p>\n<p>La programaci\u00f3n visual orientada a aplicaci\u00f3n, Las que muestran ventanas con<br \/>\nobjetos que reaccionan a los eventos del usuario. Son un serio problema pues<br \/>\ncada sistema operativo maneja sus propios apis visuales. Por ejemplo Windows y<br \/>\nGNU\/Linux con Gnome y por que no Mac OS seguramente ha de manejar su propio<br \/>\nrepertorio de apis para sus programas gr\u00e1ficos.<\/p>\n<p>\u00bfY como solucionar este problema para portar programas a cualquier plataforma<br \/>\nevitando reescribir mucho c\u00f3digo?<\/p>\n<p>1) Tus programas no deben hacer llamadas a apis propias del sistema operativo,<br \/>\ndebes usar clases escritas completamente en C# o cualquier lenguaje que soporte<br \/>\n.NET<br \/>\n2) Las soluciones ha determinados problemas deben ser escritas en clases<br \/>\ndiferentes o escribir tus m\u00e9todos o funciones estructuradamente, pide argumentos<br \/>\nde entrada y entrega el resultado de salida con un return<br \/>\n3) Aseg\u00farate que en tus clases no interact\u00faen directamente con los componentes<br \/>\nvisuales, que ninguna de tus clases o m\u00e9todos\/funciones modifique nada visual,<br \/>\ntus soluciones deben ser eso, Solucionar un problema determinado y retornar el<br \/>\nvalor de la soluci\u00f3n, ya con este valor tu puedes modificar los componentes<br \/>\nvisuales, por ejemplo modificar el texto de alg\u00fan Label o el texto de un bot\u00f3n.<\/p>\n<p>A continuaci\u00f3n escribo 2 programas que ilustran el llamado a una funci\u00f3n muy<br \/>\nsencilla. La funci\u00f3n, recibe un valor entero de entrada, lo incrementa y<br \/>\nfinalmente devuelve el valor ya incrementado. El valor incrementado es asignado<br \/>\nun bot\u00f3n. Tambi\u00e9n interact\u00faan 2 objetos visuales. Un textbox.<\/p>\n<p>El primer programa esta escrito con componentes visuales propios de Windows y el<br \/>\nsegundo con los respectivos de Gnome en GNU\/Linux, ambos programas son id\u00e9nticos<br \/>\nvisualmente y a nivel c\u00f3digo fuente trato de mantenerlos lo mas id\u00e9ntico<br \/>\nposible.<\/p>\n<p>Versi\u00f3n para MS\/Windows con Windows.Form<\/p>\n<p>using System.Windows.Forms;<\/p>\n<p>class wform<br \/>\n{<br \/>\npublic static int incremento (int contador)<br \/>\n{<br \/>\nreturn ++contador;<br \/>\n}<\/p>\n<p>public static void Main()<br \/>\n{<br \/>\n\/\/ Codigo de Ventana<br \/>\nForm ventana = new Form ();<br \/>\nventana.Disposed += delegate <br \/>\n{ <br \/>\nApplication.Exit ();<br \/>\n};<br \/>\nventana.Text = &quot;Programa C# en Windows&quot;;<\/p>\n<p>\/\/ Boton Cerrar<\/p>\n<p>Button bcerrar = new Button ();<br \/>\nbcerrar.Text = &quot;Cerrar&quot;;<br \/>\nbcerrar.Height = 30;<br \/>\nbcerrar.Width = 80;<br \/>\nbcerrar.Location = new System.Drawing.Point (110,230);<br \/>\nbcerrar.Click += delegate { Application.Exit (); };<\/p>\n<p>\/\/ Etiqueta de Texto<br \/>\nLabel etiqueta = new Label();<br \/>\netiqueta.Location = new System.Drawing.Point (70,90);<\/p>\n<p>\/\/ Caja de Texto<br \/>\nTextBox texto = new TextBox ();<br \/>\ntexto.TextChanged += delegate { etiqueta.Text = texto.Text; };<br \/>\ntexto.Location = new System.Drawing.Point (70,120);<\/p>\n<p>\/\/ Boton incrementador<br \/>\nButton bincremento = new Button ();<br \/>\nbincremento.Height = 30;<br \/>\nbincremento.Width = 80;<br \/>\nbincremento.Text = &quot;0&quot;;<br \/>\nbincremento.Click += delegate { bincremento.Text = System.Convert.ToString<br \/>\n(incremento (System.Convert.ToInt32 (bincremento.Text))); };<br \/>\nbincremento.Location = new System.Drawing.Point (70,40);<\/p>\n<p>\n\/\/ Carga final de objetos<br \/>\nventana.Height = 300;<br \/>\nventana.Width = 300;<br \/>\nventana.Controls.Add (bcerrar);<br \/>\nventana.Controls.Add (etiqueta);<br \/>\nventana.Controls.Add (texto);<br \/>\nventana.Controls.Add (bincremento);<br \/>\nventana.Show ();<\/p>\n<p>\/\/ Ejeutar el programa<br \/>\nApplication.Run ();<\/p>\n<p>}<br \/>\n}<\/p>\n<p>Versi\u00f3n para GNU\/Linux con Gnomo y mono<\/p>\n<p>using Gtk;<\/p>\n<p>class ventanagtk<br \/>\n{<\/p>\n<p>public static int incremento (int contador)<br \/>\n{<br \/>\nreturn ++contador;<br \/>\n}<\/p>\n<p>\npublic static void Main(string[] args)<br \/>\n{<br \/>\n\/\/ Inicializar componentes<br \/>\nApplication.Init ();<\/p>\n<p>\n\/\/ Codigo de Ventana<br \/>\nWindow ventana = new Window (&quot;Programa C# en Linux&quot;);<br \/>\nventana.DeleteEvent += delegate { Application.Quit (); };<\/p>\n<p>\/\/ Boton Cerrar<br \/>\nButton bcerrar = new Button (&quot;Cerrar&quot;);<br \/>\nbcerrar.HeightRequest = 30;<br \/>\nbcerrar.WidthRequest = 80;<br \/>\nbcerrar.Clicked += delegate { Application.Quit (); };<\/p>\n<p>\/\/ Etiqueta de Texto<br \/>\nLabel etiqueta = new Label();<\/p>\n<p>\/\/ Caja de Texto<br \/>\nEntry texto = new Entry ();<br \/>\ntexto.Changed += delegate { etiqueta.Text = texto.Text; }; <\/p>\n<p>\/\/ Boton incrementador<br \/>\nButton bincremento = new Button (&quot;0&quot;);<br \/>\nbincremento.HeightRequest = 30;<br \/>\nbincremento.WidthRequest = 80;<br \/>\nbincremento.Clicked += delegate { bincremento.Label = System.Convert.ToString<br \/>\n(incremento (System.Convert.ToInt32 (bincremento.Label))); };<\/p>\n<p>\n\/\/Capa de objetos<br \/>\nFixed controls = new Fixed ();<br \/>\ncontrols.Add (bcerrar);<br \/>\ncontrols.Add (texto);<br \/>\ncontrols.Add (etiqueta);<br \/>\ncontrols.Add (bincremento);<\/p>\n<p>\/\/ Control de objetos en la capa de objetos<br \/>\ncontrols.Move (bcerrar,110,230);<br \/>\ncontrols.Move (texto,70,120);<br \/>\ncontrols.Move (etiqueta,70,90);<br \/>\ncontrols.Move (bincremento,70,40);<\/p>\n<p>\/\/ Carga final de objetos<br \/>\nventana.Add (controls);<br \/>\nventana.HeightRequest = 300; \/\/Alto de ventana<br \/>\nventana.WidthRequest = 300; \/\/ Largo de ventana<br \/>\nventana.ShowAll ();<\/p>\n<p>\/\/ Ejeutar el programa<br \/>\nApplication.Run ();<br \/>\n}<br \/>\n}<\/p>\n<p>Por ultimo un video mostrando los programas de forma operativa y corriendo cada<br \/>\nuno en su propio ambiente.<\/p>\n<p><object type=\"application\/x-oleobject\" height=\"573\" standby=\"Loading Microsoft Windows Media Player components...\" width=\"624\" classid=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\" name=\"MediaPlayer\"><param NAME=\"URL\" VALUE=\"http:\/\/metacafe-113.vo.llnwd.net\/e1\/ItemFiles\/[From%20www.metacafe.com]%20227074.1609191.1.mpg\" ref><\/param><param NAME=\"rate\" VALUE=\"1\"><\/param><param NAME=\"balance\" VALUE=\"0\"><\/param><param NAME=\"currentPosition\" VALUE=\"0\"><\/param><param NAME=\"defaultFrame\" VALUE><\/param><param NAME=\"playCount\" VALUE=\"1\"><\/param><param NAME=\"autoStart\" VALUE=\"0\"><\/param><param NAME=\"currentMarker\" VALUE=\"0\"><\/param><param NAME=\"invokeURLs\" VALUE=\"-1\"><\/param><param NAME=\"baseURL\" VALUE><\/param><param NAME=\"volume\" VALUE=\"89\"><\/param><param NAME=\"mute\" VALUE=\"0\"><\/param><param NAME=\"uiMode\" VALUE=\"full\"><\/param><param NAME=\"stretchToFit\" VALUE=\"0\"><\/param><param NAME=\"windowlessVideo\" VALUE=\"0\"><\/param><param NAME=\"enabled\" VALUE=\"-1\"><\/param><param NAME=\"enableContextMenu\" VALUE=\"-1\"><\/param><param NAME=\"fullScreen\" VALUE=\"0\"><\/param><param NAME=\"SAMIStyle\" VALUE><\/param><param NAME=\"SAMILang\" VALUE><\/param><param NAME=\"SAMIFilename\" VALUE><\/param><param NAME=\"captioningID\" VALUE><\/param><param NAME=\"enableErrorDialogs\" VALUE=\"0\"><\/param><param NAME=\"_cx\" VALUE=\"8467\"><\/param><param NAME=\"_cy\" VALUE=\"8043\"><\/param><\/object><\/p>\n<p>&nbsp;<\/p>\n<p>Si no puedes ver el video, puedes ir a esta URL y descargarlo<\/p>\n<p>http:\/\/www.metacafe.com\/watch\/227074\/ejemplo_de_portar_soluciones_con_c\/<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>La importancia de C# es tener la capacidad de ser portable. Esto nos ayuda a escribir programas una vez y portarlos a cualquier plataforma sin modificaciones, sin embargo siempre hay un pero. La programaci\u00f3n visual orientada a aplicaci\u00f3n, Las que muestran ventanas con objetos que reaccionan a los eventos del usuario. Son un serio problema&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-60","post","type-post","status-publish","format-standard","hentry","category-programacion"],"_links":{"self":[{"href":"https:\/\/www.lastdragon.net\/index.php?rest_route=\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lastdragon.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lastdragon.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lastdragon.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lastdragon.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=60"}],"version-history":[{"count":0,"href":"https:\/\/www.lastdragon.net\/index.php?rest_route=\/wp\/v2\/posts\/60\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.lastdragon.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lastdragon.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lastdragon.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}