代码之家  ›  专栏  ›  技术社区  ›  Galaxylokka

找不到类型或命名空间名称“iOSBackgroundService”

  •  0
  • Galaxylokka  · 技术社区  · 11 月前

    错误消息:找不到类型或命名空间名称“iOSBackgroundService”

    我在Platform中创建了名为iOSBackgroundService的新类>网间网操作系统。但我不能把它注射到毛伊计划中。

    #elif IOS
                builder.Services.AddSingleton<IBackgroundService, iOSBackgroundService>();
    #endif
    

    我无法创建这样的对象来传递相同的信息。

    var iOSBck = new iOSBackgroundService();
    

    但当我创建默认对象(或注入)时 AppDelegate 放置在同一目录中的类(平台->iOS)。这没有显示任何错误。

    事实上,我对此一无所知。如有任何帮助,不胜感激。

    这是mauiprogram.cs的完整代码,供您参考。

    **using Microsoft.Extensions.Logging;
    using Plugin.Fingerprint.Abstractions;
    using Plugin.Fingerprint;
    using Plugin.Maui.Biometric;
    using HR_Attendance_MAUI.Services;
    
    #if ANDROID
    using HR_Attendance_MAUI.Platforms.Android.Services;
    #elif IOS
    using HR_Attendance_MAUI.Platforms.iOS; 
    #endif
    
    
    namespace HR_Attendance_MAUI
    {
        public static class MauiProgram
        {
            public static MauiApp CreateMauiApp()
            {
                var builder = MauiApp.CreateBuilder();
                builder
                    .UseMauiApp<App>()
                    .ConfigureFonts(fonts =>
                    {
                        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                        fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                    });
                builder.Services.AddSingleton<MainPage>();
                builder.Services.AddSingleton<FinPrintReg_Page>();
                builder.Services.AddSingleton(typeof(IFingerprint), CrossFingerprint.Current);
    
    #if DEBUG
                builder.Logging.AddDebug();
    #endif
    #if ANDROID
            builder.Services.AddSingleton<IBackgroundService, AndroidBackgroundService>();
    #elif IOS
                builder.Services.AddSingleton<IBackgroundService, iOSBackgroundService>();
    #endif
    
                return builder.Build();
            }
        }
    
    
    }**
    

    这是iOSBackgroundService的代码

    using Foundation;
    using UIKit;
    using HR_Attendance_MAUI.Services;
    using HR_Attendance_MAUI.Data;
    using HR_Attendance_MAUI.Model;
    using System.Threading.Tasks;
    using System.Collections.Generic;
    using System;
    using System.Net.Http;
    using System.Net.Http.Json;
    using System.IO;
    using Microsoft.Maui.Controls;
    using HR_Attendance_MAUI.Platforms.iOS.Services;
    
    [assembly: Dependency(typeof(iOSBackgroundService))]
    
    namespace HR_Attendance_MAUI.Platforms.iOS.Services
    {
        [Register("iOSBackgroundService")]
        public class iOSBackgroundService : NSObject, IBackgroundService
        {
            private nint _taskId;
            private Timer _timer;
            private AttendanceDatabaseService2 _attendanceDatabaseService;
            private readonly TimeSpan _delay = TimeSpan.FromMinutes(5);
    
            public static bool IsForegroundServiceRunning;
    
            public void Start()
            {
                IsForegroundServiceRunning = true;
                _taskId = UIApplication.SharedApplication.BeginBackgroundTask("BackgroundService", OnExpiration);
    
                Task.Run(async () =>
                {
                    while (IsForegroundServiceRunning)
                    {
                        var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AttendanceDB.db3");
                        _attendanceDatabaseService = new AttendanceDatabaseService2(dbPath);
                        List<AttendanceData> attendanceDataList = _attendanceDatabaseService.GetAllAttendances();
    
                        if (attendanceDataList.Count == 0)
                        {
                            Stop();
                        }
    
                        var networkAccess = Connectivity.NetworkAccess;
                        if (networkAccess == NetworkAccess.Internet)
                        {
                            bool result = await SyncAttendanceData();
                        }
    
                        Console.WriteLine("Background Service is Running");
                        await Task.Delay(_delay);
                    }
    
                    UIApplication.SharedApplication.EndBackgroundTask(_taskId);
                    _taskId = UIApplication.BackgroundTaskInvalid;
                });
            }
    
            public void Stop()
            {
                IsForegroundServiceRunning = false;
                UIApplication.SharedApplication.EndBackgroundTask(_taskId);
            }
    
            public bool IsForeGroundServiceRunning()
            {
                return IsForegroundServiceRunning;
            }
            private void OnExpiration()
            {
                Stop();
            }
    
            private async Task<bool> SyncAttendanceData()
            {
                var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AttendanceDB.db3");
                _attendanceDatabaseService = new AttendanceDatabaseService2(dbPath);
                List<AttendanceData> attendanceDataList = _attendanceDatabaseService.GetAllAttendances();
    
                if (attendanceDataList.Count == 0)
                {
                    return false;
                }
    
                foreach (AttendanceData attendanceData in attendanceDataList)
                {
                    string inTimeDate = attendanceData.inTimeDate;
                    string inTimeDate1 = attendanceData.outTime;
                    string inTimeDate2 = attendanceData.lonOut;
                    string inTimeDate3 = attendanceData.latOut;
                }
    
                DateTimeOffset currentTime1 = DateTimeOffset.Now;
                DateTime currentTime = currentTime1.DateTime;
                currentTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 0, 0, 0);
                string currentDate = currentTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
    
                HttpClient client;
                try
                {
                    client = HttpClientFactory.CreateHttpClient();
                    var response = await client.PostAsJsonAsync("api/Attendance/StoreList", attendanceDataList);
    
                    if (response.IsSuccessStatusCode)
                    {
                        _attendanceDatabaseService.StoreTodaysRecordsAndDeleteAll(currentDate);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    string errorMessage = ex.Message;
                    await App.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
                    return false;
                }
            }
        }
    }
    

    这是IBackgroundService接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace HR_Attendance_MAUI.Services
    {
        public interface IBackgroundService
        {
            void Start();
            void Stop();
    
            bool IsForeGroundServiceRunning();
        }
    }
    
    1 回复  |  直到 11 月前
        1
  •  1
  •   Alexandar May - MSFT    11 月前

    错误消息:类型或命名空间名称“iOSBackgroundService” 找不到

    根据错误日志,您可以通过更改类名来修复它 HR_Attendance_MAUI.Platforms.iOS.Services HR_Attendance_MAUI.Platforms.iOS 在下面 Platform -> iOS .