可以';t将一个函数从一个类引用到另一个类WP8

本文关键字:一个 引用 另一个 WP8 函数 可以 | 更新日期: 2023-09-27 18:30:01

我想将一个函数从一个类引用到另一个类。

我有第一类:

using Jumper.Core.Model;
using Jumper.Core.PlatformServices;
using Jumper.Core.PlatformServices.Storage;
using Jumper.Core.Services;
using System;
using System.Globalization;
using System.Threading.Tasks;
namespace Jumper.Core.Client
{
public sealed class Client
     <TBaseSaveData,
    TDeviceServiceHelper,
    TGeoLocationService,
    TNetworkService>
    where TBaseSaveData : BaseSaveDataService, new()
    where TDeviceServiceHelper : IDeviceServiceHelper, new()
    where TGeoLocationService : IGeoLocationService, new()
    where TNetworkService : INetworkService, new()
{
    private readonly int appId;
    private readonly string appVersion;
    public readonly ServiceFactory<TBaseSaveData,
        TDeviceServiceHelper,
        TGeoLocationService,
        TNetworkService> serviceFactory;
    #region Constructors
    internal Client
        (int appId, string appVersion)
    {
        this.appId = appId;
        this.appVersion = appVersion;
        this.serviceFactory = new ServiceFactory<TBaseSaveData,
        TDeviceServiceHelper,
        TGeoLocationService,
        TNetworkService>();
    }
    #endregion
public async Task<TrackingItem> CreateDefaultItem()
    {
        Tuple<double, double> location = await this.serviceFactory.GeoLocationService
            .GetUnifiedGeoLocation();
        var assemblyService = this.serviceFactory.AssemblyInfoService;
        var deviceService = this.serviceFactory.DeviceServiceHelper;
        var seconds = (DateTime.UtcNow -        this.serviceFactory.TrackingService.StartTime).TotalSeconds;
        TrackingItem item = new TrackingItem()
        {
            //APP
            AppId = this.appId,
            RunningSeconds = (int)seconds,
            AppVersion = this.appVersion,
            Language = CultureInfo.CurrentCulture.TwoLetterISOLanguageName,
            Country = CultureInfo.CurrentCulture.Name.Substring(3, 2),
        };
        return item;
    }

我想在其他类中使用函数CreateDefaultItem(),但我不能。。。这就是我所做的和不起作用的:

using Jumper.Core;
...
...
Jumper.Core.Client.CreateDefaultItem();

在另一个类上使用函数CreateDefaultItem需要做什么?

谢谢!

可以';t将一个函数从一个类引用到另一个类WP8

而不是这个:

public async Task<TrackingItem> CreateDefaultItem()

你需要这个:

public static async Task<TrackingItem> CreateDefaultItem()

注意添加的"static"关键字。这使得方法调用独立于类的任何特定实例。

然而,缺点是方法体只能使用类的其他静态成员(静态字段、静态属性、静态方法)。