Home > Net >  How to convert CompasData to Int?
How to convert CompasData to Int?

Time:10-27

I want to use .NET MAUI compass data inside if statement, but I can't use it just by it's self. I need to convert it to int or double.

Code:

namespace MauiApp1;

public partial class MainPage : ContentPage
{

    public MainPage()
    {
        InitializeComponent();
    }

    private void ToggleCompass()
    {
        if (Compass.Default.IsSupported)
        {
            if (!Compass.Default.IsMonitoring)
            {
                // Turn on compass
                Compass.Default.ReadingChanged  = Compass_ReadingChanged;
                Compass.Default.Start(SensorSpeed.UI);
            }
            else
            {
                // Turn off compass
                Compass.Default.Stop();
                Compass.Default.ReadingChanged -= Compass_ReadingChanged;
            }
        }
    }

    private void Compass_ReadingChanged(object sender, CompassChangedEventArgs e)
    {
        // Update UI Label with compass state
        this.north.Text = $"Compass: {Convert.ToInt32(e.Reading)}";
        //if (value >= 355 || value <= 5) this.arrow.RotateTo(0);
        //if (value >= 5 || value <= 85) this.arrow.RotateTo(45);
        //if (value >= 85 || value <= 95) this.arrow.RotateTo(90);
        //if (value >= 95 || value <= 175) this.arrow.RotateTo(135);
        //if (value >= 175 || value <= 185) this.arrow.RotateTo(180);
        //if (value >= 185 || value <= 265) this.arrow.RotateTo(225);
        //if (value >= 265 || value <= 275) this.arrow.RotateTo(270);
        //if (value >= 275 || value <= 355) this.arrow.RotateTo(315);

    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        ToggleCompass();
    }
}

I tried ".Convert", ".Int32.Parse", "Double.Parse" - nothing works! Is there any others ways to parse it? I need it to be Int to use in if statement.

CodePudding user response:

var heading = e.Reading.HeadingMagneticNorth;
int value = (int)heading;
  • Related