nm9505

Compas Android XAMARIN FORMS (GUI)

Aug 11th, 2025
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.67 KB | Science | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Xamarin.Essentials;
  5. using Xamarin.Forms;
  6.  
  7. namespace BrujulaAndroid
  8. {
  9.     public partial class MainPage : ContentPage
  10.     {
  11.         public MainPage()
  12.         {
  13.             InitializeComponent();
  14.         }
  15.         public static void Main()
  16.         {  
  17.             string[] imgs;
  18.             TapGestureRecognizer imageTapGesture = new TapGestureRecognizer();
  19.             TapGestureRecognizer labelTapGesture = new TapGestureRecognizer();
  20.             //Inicio de Método anónimo del controlador de interfaz gráfica
  21.             Ui.RunOnUiThread(()=>
  22.             {
  23.                 imgs = new string[5]; int sn=3; int ar=1;
  24.                 imgs[0]="assets/files/preciosaex1.png";
  25.                 imgs[1]="assets/files/preciosaex2.png";
  26.                 imgs[2]="assets/files/preciosaex3.png";
  27.                 imgs[3]="assets/files/preciosaex4.png";
  28.                 imgs[4]="assets/files/preciosaex5.png";
  29.                 Random sourceNumber = new Random();
  30.                 //Creando contenedor principal de elementos visuales
  31.                 StackLayout stack = new StackLayout();
  32.                 stack.Orientation = StackOrientation.Vertical;
  33.                 Ui.Orientation = XamarinFormsUiLib.ScreenOrientation.Portrait;
  34.                 stack.BackgroundColor=Color.Black;
  35.                 //Creando contenedor vertical
  36.                 var stackV = new StackLayout()
  37.                 {
  38.                     Scale = 0.8,
  39.                     Orientation = StackOrientation.Vertical,
  40.                     HorizontalOptions = LayoutOptions.FillAndExpand,
  41.                     VerticalOptions = LayoutOptions.FillAndExpand
  42.                 };
  43.                 //Creando contenedor horizontal.
  44.                 var stackH = new StackLayout()
  45.                 {
  46.                     Orientation = StackOrientation.Horizontal,
  47.                     HorizontalOptions = LayoutOptions.FillAndExpand,
  48.                     VerticalOptions = LayoutOptions.FillAndExpand,
  49.                     Scale = 0.9
  50.                 };
  51.                 var title = new Label()
  52.                 {
  53.                     Text="\u2003\u2003\u2003\t\tCompás Naútico",
  54.                     FontSize=23,
  55.                     TextColor=Color.Aqua,
  56.                     FontAttributes=FontAttributes.Bold
  57.                 };
  58.                 var info = new Label()
  59.                 {
  60.                     Text="\t\t\t\t\t\t\t\t\t[email protected]",
  61.                     FontSize=17,
  62.                     TextColor=Color.GreenYellow,
  63.                     TextDecorations = TextDecorations.Underline,
  64.                     FontAttributes=FontAttributes.Bold
  65.                 };
  66.                 labelTapGesture.Tapped += OnLinkTapped;
  67.                 async void OnLinkTapped (object sender, EventArgs linkargs)
  68.                 {
  69.                     await Browser.OpenAsync("WEB LINK", BrowserLaunchMode.SystemPreferred);
  70.                 }
  71.                 info.GestureRecognizers.Add(labelTapGesture);
  72.                 var info2 = new Label()
  73.                 {
  74.                     Text="\u2003\u2003\t\tColocar el dispositivo horizontalmente",
  75.                     FontSize=15,
  76.                     TextColor=Color.LightYellow,
  77.                     FontAttributes=FontAttributes.Bold
  78.                 };
  79.                 //Fijando el pin marcador mediante Label
  80.                 var pin = new Label()
  81.                 {
  82.                     Text = "\u21A7",
  83.                     FontSize = 33,
  84.                     Margin = new Thickness(0,0,0,0),
  85.                     FontAttributes = FontAttributes.Bold,
  86.                     HorizontalOptions = LayoutOptions.Center
  87.                 };
  88.                 //Enlazando el TextColorProperty del puntero.
  89.                 pin.SetBinding(Label.TextColorProperty, "Color.Red");
  90.                 var north = new Label()
  91.                 {
  92.                     Text = "N",
  93.                     FontSize = 27,
  94.                     FontAttributes = FontAttributes.Bold,
  95.                     HorizontalOptions = LayoutOptions.Center
  96.                 };
  97.                 north.SetBinding(Label.TextColorProperty, "Color.Yellow");
  98.                 var south = new Label()
  99.                 {
  100.                     Text = "S",
  101.                     FontSize = 30,
  102.                     FontAttributes = FontAttributes.Bold,
  103.                     HorizontalOptions = LayoutOptions.Center
  104.                 };
  105.                 south.SetBinding(Label.TextColorProperty, "Color.Aqua");
  106.                 var west = new Label()
  107.                 {
  108.                     Text = "O",
  109.                     FontSize = 30,
  110.                     FontAttributes = FontAttributes.Bold,
  111.                     VerticalOptions = LayoutOptions.Center,
  112.                     HorizontalOptions = LayoutOptions.StartAndExpand
  113.                 };
  114.                  west.SetBinding(Label.TextColorProperty, "Color.Aqua");
  115.                 var east = new Label()
  116.                 {
  117.                     Text = "E",
  118.                     FontSize = 30,
  119.                     FontAttributes = FontAttributes.Bold,
  120.                     VerticalOptions = LayoutOptions.Center,
  121.                     HorizontalOptions = LayoutOptions.EndAndExpand
  122.                 };
  123.                  east.SetBinding(Label.TextColorProperty, "Color.Aqua");
  124.                 var bear = new Label()
  125.                 {
  126.                     Text = ".",
  127.                     TextColor=Color.Yellow,
  128.                     FontAttributes=FontAttributes.Bold,
  129.                     FontSize = 28,
  130.                     BackgroundColor=Color.Blue,
  131.                     MinimumWidthRequest = 300,
  132.                     MinimumHeightRequest = 300,
  133.                     VerticalOptions = LayoutOptions.CenterAndExpand,
  134.                     HorizontalOptions = LayoutOptions.CenterAndExpand
  135.                 };
  136.                 var Referencia = new Label()
  137.                 {
  138.                     TextColor = Color.White,
  139.                     FontSize = 20,
  140.                     BackgroundColor = Color.Blue,
  141.                     FontAttributes = FontAttributes.Bold,
  142.                     VerticalOptions = LayoutOptions.Center,
  143.                     HorizontalOptions = LayoutOptions.CenterAndExpand
  144.                 };
  145.                 Referencia.SetBinding(Label.TextProperty, "Norte");
  146.                 Image Espego = new Image
  147.                 {
  148.                    
  149.                     Source = "assets/files/preciosaex5.png",
  150.                 };
  151.                 imageTapGesture.Tapped += OnImageTapped;
  152.                 void OnImageTapped (object sender, EventArgs args)
  153.                 {
  154.                     InTap:
  155.                     ar=sn;
  156.                     sn = sourceNumber.Next(0,5);
  157.                     if (sn == ar) goto InTap;
  158.                     Espego.Source= imgs[sn];
  159.                 }
  160.                 Espego.GestureRecognizers.Add(imageTapGesture);
  161.                 var di = DeviceDisplay.MainDisplayInfo;
  162.                
  163.                 //Método de cálculo de parámetros de elementos gráficos
  164.                 void Translate()
  165.                 {
  166.                     pin.TranslationY = di.Height / di.Density / 9;
  167.                     north.TranslationY = di.Height / di.Density / 9;
  168.                     south.TranslationY = -(di.Height / di.Density / 9);
  169.                 }
  170.                 Translate(); //Llamada del método paramétrico.
  171.                 //Evento de llamada y visualización.
  172.                 DeviceDisplay.MainDisplayInfoChanged += (sdr, args) =>
  173.                 {
  174.                     di = args.DisplayInfo;
  175.                     Translate();
  176.                 };
  177.                 //Agregando elementos en su orden a los contenedores.
  178.                 stackH.Children.Add(west);
  179.                 stackH.Children.Add(bear);
  180.                 stackH.Children.Add(east);
  181.                
  182.                 stackV.Children.Add(north);
  183.                 stackV.Children.Add(stackH);
  184.                 stackV.Children.Add(south);
  185.                
  186.                 stack.Children.Add(title);
  187.                 stack.Children.Add(info);
  188.                 stack.Children.Add(info2);
  189.                 stack.Children.Add(Espego);
  190.                
  191.                 stack.Children.Add(pin);
  192.                 stack.Children.Add(Referencia);
  193.                 stack.Children.Add(stackV);
  194.                 //Suscribiendo elementos al controlador de datos
  195.                 Compass.ReadingChanged += (sdr, e) =>
  196.                 {
  197.                     stackV.Rotation = -e.Reading.HeadingMagneticNorth;
  198.                     //Manteniendo inmóvil el centro de rotación.
  199.                     bear.Rotation = e.Reading.HeadingMagneticNorth;
  200.                     bear.Text = e.Reading.HeadingMagneticNorth.ToString("0°");
  201.                     double Lecture = Convert.ToDouble((bear.Text).TrimEnd('°'));
  202.                     if(bear.Text!="0°")
  203.                     {
  204.                         north.TextColor=Color.Yellow;
  205.                         pin.TextColor=Color.Red;
  206.                     }
  207.                     if(bear.Text!="90°")
  208.                     {
  209.                         east.TextColor=Color.Blue;
  210.                         pin.TextColor=Color.Red;
  211.                     }
  212.                     if(bear.Text!="180°")
  213.                     {
  214.                         south.TextColor=Color.Blue;
  215.                         pin.TextColor=Color.Red;
  216.                     }
  217.                     if(bear.Text!="270°")
  218.                     {
  219.                         west.TextColor=Color.Blue;
  220.                         pin.TextColor=Color.Red;
  221.                     }
  222.                     if (bear.Text == "0°" || bear.Text == "360°")
  223.                     {
  224.                         north.TextColor=Color.Red;
  225.                         pin.TextColor = Color.Red;
  226.                     }
  227.                     if (bear.Text == "90°")
  228.                     {
  229.                         east.TextColor=Color.Yellow;
  230.                         pin.TextColor=Color.Yellow;
  231.                     }
  232.                     if (bear.Text == "180°")
  233.                     {
  234.                         south.TextColor=Color.Yellow;
  235.                         pin.TextColor=Color.Yellow;
  236.                     }
  237.                     if (bear.Text == "270°")
  238.                     {
  239.                         west.TextColor=Color.Yellow;
  240.                         pin.TextColor=Color.Yellow;
  241.                     }
  242.                     if (Lecture==0 || Lecture==360) Referencia.Text="Norte";
  243.                     if (Lecture==90) Referencia.Text="Este";
  244.                     if (Lecture==180) Referencia.Text="Sur";
  245.                     if (Lecture==270) Referencia.Text="Oeste";
  246.                     if (0<Lecture & Lecture<90)
  247.                     {
  248.                         Referencia.Text="NorEste";
  249.                     }
  250.                     if (90<Lecture & Lecture<180)
  251.                     {
  252.                         Referencia.Text="SurEste";
  253.                     }
  254.                     if (180<Lecture & Lecture<270)
  255.                     {
  256.                         Referencia.Text="SurOeste";
  257.                     }
  258.                     if (270<Lecture & Lecture<360)
  259.                     {
  260.                         Referencia.Text="NorOeste";
  261.                     }
  262.                 };
  263.                 //Iniciando lectura del compás.
  264.                 Compass.Start(SensorSpeed.UI);
  265.                 //Cargando la vista de la interfaz visual.
  266.                 Ui.LoadMainLayout(stack);
  267.             });
  268.             //Fin del evento anónimo de interfaz gráfica (GUI).
  269.         }
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment