Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- namespace BrujulaAndroid
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- }
- public static void Main()
- {
- string[] imgs;
- TapGestureRecognizer imageTapGesture = new TapGestureRecognizer();
- TapGestureRecognizer labelTapGesture = new TapGestureRecognizer();
- //Inicio de Método anónimo del controlador de interfaz gráfica
- Ui.RunOnUiThread(()=>
- {
- imgs = new string[5]; int sn=3; int ar=1;
- imgs[0]="assets/files/preciosaex1.png";
- imgs[1]="assets/files/preciosaex2.png";
- imgs[2]="assets/files/preciosaex3.png";
- imgs[3]="assets/files/preciosaex4.png";
- imgs[4]="assets/files/preciosaex5.png";
- Random sourceNumber = new Random();
- //Creando contenedor principal de elementos visuales
- StackLayout stack = new StackLayout();
- stack.Orientation = StackOrientation.Vertical;
- Ui.Orientation = XamarinFormsUiLib.ScreenOrientation.Portrait;
- stack.BackgroundColor=Color.Black;
- //Creando contenedor vertical
- var stackV = new StackLayout()
- {
- Scale = 0.8,
- Orientation = StackOrientation.Vertical,
- HorizontalOptions = LayoutOptions.FillAndExpand,
- VerticalOptions = LayoutOptions.FillAndExpand
- };
- //Creando contenedor horizontal.
- var stackH = new StackLayout()
- {
- Orientation = StackOrientation.Horizontal,
- HorizontalOptions = LayoutOptions.FillAndExpand,
- VerticalOptions = LayoutOptions.FillAndExpand,
- Scale = 0.9
- };
- var title = new Label()
- {
- Text="\u2003\u2003\u2003\t\tCompás Naútico",
- FontSize=23,
- TextColor=Color.Aqua,
- FontAttributes=FontAttributes.Bold
- };
- var info = new Label()
- {
- Text="\t\t\t\t\t\t\t\t\t[email protected]",
- FontSize=17,
- TextColor=Color.GreenYellow,
- TextDecorations = TextDecorations.Underline,
- FontAttributes=FontAttributes.Bold
- };
- labelTapGesture.Tapped += OnLinkTapped;
- async void OnLinkTapped (object sender, EventArgs linkargs)
- {
- await Browser.OpenAsync("WEB LINK", BrowserLaunchMode.SystemPreferred);
- }
- info.GestureRecognizers.Add(labelTapGesture);
- var info2 = new Label()
- {
- Text="\u2003\u2003\t\tColocar el dispositivo horizontalmente",
- FontSize=15,
- TextColor=Color.LightYellow,
- FontAttributes=FontAttributes.Bold
- };
- //Fijando el pin marcador mediante Label
- var pin = new Label()
- {
- Text = "\u21A7",
- FontSize = 33,
- Margin = new Thickness(0,0,0,0),
- FontAttributes = FontAttributes.Bold,
- HorizontalOptions = LayoutOptions.Center
- };
- //Enlazando el TextColorProperty del puntero.
- pin.SetBinding(Label.TextColorProperty, "Color.Red");
- var north = new Label()
- {
- Text = "N",
- FontSize = 27,
- FontAttributes = FontAttributes.Bold,
- HorizontalOptions = LayoutOptions.Center
- };
- north.SetBinding(Label.TextColorProperty, "Color.Yellow");
- var south = new Label()
- {
- Text = "S",
- FontSize = 30,
- FontAttributes = FontAttributes.Bold,
- HorizontalOptions = LayoutOptions.Center
- };
- south.SetBinding(Label.TextColorProperty, "Color.Aqua");
- var west = new Label()
- {
- Text = "O",
- FontSize = 30,
- FontAttributes = FontAttributes.Bold,
- VerticalOptions = LayoutOptions.Center,
- HorizontalOptions = LayoutOptions.StartAndExpand
- };
- west.SetBinding(Label.TextColorProperty, "Color.Aqua");
- var east = new Label()
- {
- Text = "E",
- FontSize = 30,
- FontAttributes = FontAttributes.Bold,
- VerticalOptions = LayoutOptions.Center,
- HorizontalOptions = LayoutOptions.EndAndExpand
- };
- east.SetBinding(Label.TextColorProperty, "Color.Aqua");
- var bear = new Label()
- {
- Text = ".",
- TextColor=Color.Yellow,
- FontAttributes=FontAttributes.Bold,
- FontSize = 28,
- BackgroundColor=Color.Blue,
- MinimumWidthRequest = 300,
- MinimumHeightRequest = 300,
- VerticalOptions = LayoutOptions.CenterAndExpand,
- HorizontalOptions = LayoutOptions.CenterAndExpand
- };
- var Referencia = new Label()
- {
- TextColor = Color.White,
- FontSize = 20,
- BackgroundColor = Color.Blue,
- FontAttributes = FontAttributes.Bold,
- VerticalOptions = LayoutOptions.Center,
- HorizontalOptions = LayoutOptions.CenterAndExpand
- };
- Referencia.SetBinding(Label.TextProperty, "Norte");
- Image Espego = new Image
- {
- Source = "assets/files/preciosaex5.png",
- };
- imageTapGesture.Tapped += OnImageTapped;
- void OnImageTapped (object sender, EventArgs args)
- {
- InTap:
- ar=sn;
- sn = sourceNumber.Next(0,5);
- if (sn == ar) goto InTap;
- Espego.Source= imgs[sn];
- }
- Espego.GestureRecognizers.Add(imageTapGesture);
- var di = DeviceDisplay.MainDisplayInfo;
- //Método de cálculo de parámetros de elementos gráficos
- void Translate()
- {
- pin.TranslationY = di.Height / di.Density / 9;
- north.TranslationY = di.Height / di.Density / 9;
- south.TranslationY = -(di.Height / di.Density / 9);
- }
- Translate(); //Llamada del método paramétrico.
- //Evento de llamada y visualización.
- DeviceDisplay.MainDisplayInfoChanged += (sdr, args) =>
- {
- di = args.DisplayInfo;
- Translate();
- };
- //Agregando elementos en su orden a los contenedores.
- stackH.Children.Add(west);
- stackH.Children.Add(bear);
- stackH.Children.Add(east);
- stackV.Children.Add(north);
- stackV.Children.Add(stackH);
- stackV.Children.Add(south);
- stack.Children.Add(title);
- stack.Children.Add(info);
- stack.Children.Add(info2);
- stack.Children.Add(Espego);
- stack.Children.Add(pin);
- stack.Children.Add(Referencia);
- stack.Children.Add(stackV);
- //Suscribiendo elementos al controlador de datos
- Compass.ReadingChanged += (sdr, e) =>
- {
- stackV.Rotation = -e.Reading.HeadingMagneticNorth;
- //Manteniendo inmóvil el centro de rotación.
- bear.Rotation = e.Reading.HeadingMagneticNorth;
- bear.Text = e.Reading.HeadingMagneticNorth.ToString("0°");
- double Lecture = Convert.ToDouble((bear.Text).TrimEnd('°'));
- if(bear.Text!="0°")
- {
- north.TextColor=Color.Yellow;
- pin.TextColor=Color.Red;
- }
- if(bear.Text!="90°")
- {
- east.TextColor=Color.Blue;
- pin.TextColor=Color.Red;
- }
- if(bear.Text!="180°")
- {
- south.TextColor=Color.Blue;
- pin.TextColor=Color.Red;
- }
- if(bear.Text!="270°")
- {
- west.TextColor=Color.Blue;
- pin.TextColor=Color.Red;
- }
- if (bear.Text == "0°" || bear.Text == "360°")
- {
- north.TextColor=Color.Red;
- pin.TextColor = Color.Red;
- }
- if (bear.Text == "90°")
- {
- east.TextColor=Color.Yellow;
- pin.TextColor=Color.Yellow;
- }
- if (bear.Text == "180°")
- {
- south.TextColor=Color.Yellow;
- pin.TextColor=Color.Yellow;
- }
- if (bear.Text == "270°")
- {
- west.TextColor=Color.Yellow;
- pin.TextColor=Color.Yellow;
- }
- if (Lecture==0 || Lecture==360) Referencia.Text="Norte";
- if (Lecture==90) Referencia.Text="Este";
- if (Lecture==180) Referencia.Text="Sur";
- if (Lecture==270) Referencia.Text="Oeste";
- if (0<Lecture & Lecture<90)
- {
- Referencia.Text="NorEste";
- }
- if (90<Lecture & Lecture<180)
- {
- Referencia.Text="SurEste";
- }
- if (180<Lecture & Lecture<270)
- {
- Referencia.Text="SurOeste";
- }
- if (270<Lecture & Lecture<360)
- {
- Referencia.Text="NorOeste";
- }
- };
- //Iniciando lectura del compás.
- Compass.Start(SensorSpeed.UI);
- //Cargando la vista de la interfaz visual.
- Ui.LoadMainLayout(stack);
- });
- //Fin del evento anónimo de interfaz gráfica (GUI).
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment