最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to correctly access motion data on IOS - Stack Overflow

programmeradmin2浏览0评论

I am trying to create a stepcounter app for both IOS and Android in a single codebase using C# MAUI in Visual Studio. Managed to get the UI working, but whenever I try and access the motiondata on IOS, the app simply crashes.

MainPage.Xaml.cs

using Plugin.Maui.Pedometer;

namespace Wodia2;
public partial class MainPage : ContentPage
{
    readonly IPedometer pedometer;

    public MainPage(IPedometer pedometer)
    {
        InitializeComponent();

        this.pedometer = pedometer;
        pedometer.ReadingChanged += Pedometer_ReadingChanged;

        IsSupported.Text = $"Is supported: {pedometer.IsSupported}";
        StartStopMonitoring.IsEnabled = pedometer.IsSupported;

    }

    void Pedometer_ReadingChanged(object sender, PedometerData e)
    {
        MainThread.InvokeOnMainThreadAsync(() =>
        {
            StepCount.Text = e.NumberOfSteps.ToString();
            LastUpdate.Text = $"Last update: {e.Timestamp}";
        });
    }

    void Button_Clicked(object sender, EventArgs e)
    {
        if (!pedometer.IsMonitoring)
        {
            pedometer.Start();
        }
        else
        {
            pedometer.Stop();
        }

        UpdateIsMonitoring();
    }

    void UpdateIsMonitoring()
    {
        IsMonitoring.Text = $"Is monitoring: {pedometer.IsMonitoring}";
    }
}

MainPage.Xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns=";
             xmlns:x=";
             x:Class="Wodia2.MainPage">

    <VerticalStackLayout Spacing="10">
        <Label x:Name="IsSupported" Text="" HorizontalOptions="Center" Margin="0,10,0,0" />
        <Label x:Name="IsMonitoring" Text="Is monitoring: False" HorizontalOptions="Center" />

        <Button x:Name="StartStopMonitoring" Text="Start/Stop" Clicked="Button_Clicked" Margin="20,20,20,0"/>
        <Label Text="Steps taken 
发布评论

评论列表(0)

  1. 暂无评论