gpt4 book ai didi

c# - 从 AppDelegate 访问类的现有实例中的方法

转载 作者:行者123 更新时间:2023-11-28 21:11:34 25 4
gpt4 key购买 nike

我需要传递一些我在应用程序返回前台时获取的数据,我已经设法触发了该方法,但我无法弄清楚如何在我的 ViewController 的现有实例中触发它,而不是制作一个新实例。

map .cs

 public delegate void beginRefreshMapLine(ReturnRouteTaken returnRouteTaken);

public void updateRouteList(ReturnRouteTaken returnRouteData)
{
coordList = new List<CLLocationCoordinate2D>();
foreach(GPSData point in returnRouteData.GPSData)
{
coordList.Add(new CLLocationCoordinate2D { Latitude = double.Parse(point.Lat), Longitude = double.Parse(point.Lng) });

updateMap(this, new EventArgs());
}
}

这是我需要在 AppDelegate.cs 的当前实例中触发的方法

AppDelegate.cs

if (GlobalVar.BoolForKey("trackMe"))
{
ReturnRouteTaken returnRouteData = webtools.GetRouteTaken(new ReturnRouteTaken() { TestDriveID = GlobalVar.IntForKey("routeTrackedID") });
if (returnRouteData.GPSData.Count > 0)
{

}
}

这是我卡住的地方,我已经尝试查看委托(delegate)并以这种方式调用方法,但我无法理解如何实现它。任何帮助将不胜感激

最佳答案

我将其标记为可能的 dupe ,但该线程在 Obj-C 中,但是可以使用 Xamarin.iOS 轻松应用相同的概念。

只需创建一个单例类,将 UIViewControllersarrayList 作为该类中的属性,每次实例化一个新的 ViewController ,将其添加到 arrayList,但还要确保从 arrayList 中删除 View Controller > 当 View Controller 被释放时。

例如你的单例可能看起来像:

public class ViewControllerHolder
{
// make constructor private to force use of Instance property
// to create and get the instance.
private ViewControllerHolder()
{
}

private static ViewControllerHolder _instance;
public static ViewControllerHolder Instance
{
get
{
if (_instance == null)
{
_instance = new ViewControllerHolder();
_instance.Controllers = new List<UIViewController>();
}
return _instance;
}
}

public List<UIViewController> Controllers { get; private set; }

}

然后您始终可以使用 ViewControllerHolder.Instance.Controllers 访问 View Controller 的 List 并对其执行任何添加或删除操作。

如果您真的只对一个 View Controller 感兴趣,那么只需在实例化时将该 View Controller 添加到 List 中,但是当不再需要 View Controller 时请将其删除,这样您就不需要不要尝试访问已处置的 View Controller ,以便在不再使用时可以对 View Controller 进行垃圾回收。

关于c# - 从 AppDelegate 访问类的现有实例中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42869249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com