如何从 iOS AppDelegate.cs 中的方法访问共享项目数据对象? 表单 App.csiOS AppDelegate.cs

问题描述

我正在尝试从我的共享项目中传递一些数据值以通过 iOS AppDelegate.cs 方法进行访问。我不想在这里讲太多细节,因为我不想限制这个问题的范围。但是该方法可以在任何时候被调用并用于获取有关应用程序的状态信息,例如isLoggedIn等

我们正在使用 galaSoft.MvvmLight.Ioc.SimpleIoc 并有一个 Customviewmodelbase,但这可能不太相关。

这些值主要是我们的 Customviewmodelbase 的一部分,我想我可以在 App.Xaml.cs 上创建某种全局对象,这可以在 AppDelegate.cs 中访问

这是我所拥有的...

using galaSoft.MvvmLight.Ioc;
using ourapp.DTO;
using ourapp.Interfaces;
using ourapp.viewmodels;

namespace ourapp.Helpers
{
    public class UITestingHelper : CustomviewmodelBase,iuiTestingHelper
    {
        [PreferredConstructor]
        public UITestingHelper(
            ICustomNavigationService navigationService,Iapiclient apiclient,IDependencyService dependencyService)
            : base(
                  navigationService,apiclient,dependencyService)
        {

        }

//

        UITestingBackdoor _status;
        public UITestingBackdoor Status
        {
            get
            {
                //var vm = (CustomviewmodelBase)App.viewmodelLocator.Resolve(
                //      typeof(CustomviewmodelBase));

                _status = new UITestingBackdoor()
                {
                    WillShowAccountPopup = base.HasMoreThanOneAccount,AppUpdateAvailable = base.AppUpdateAvailable,IsLoggedIn = App.IsLoggedIn,IsConnected = App.Connectivity.IsConnected,};

                return _status;
            }
        }

        public string GetAppStatus()
        {
            string json = Newtonsoft.Json.JsonConvert
                .SerializeObject(Status);

            return json;
        }
    }
}

这是我的 AppDelegate.cs 方法...

        [Export("UITestBackDoor:")]
        public Nsstring UITestBackDoor(Nsstring value)
        {
            var status = App.UITestingStatus.GetAppStatus();

            return (Nsstring)status;
        }

然而,对象基本上是一个拥有自己权利的视图模型,并且具有依赖注入来初始化它。但是,它没有针对特定视图注册,因此无法解析。

我的确切问题是,虽然我的 Customviewmodelbase 上的属性正在获取它的值集。在我的全局对象中访问这些值时,该值为空。

我相信这与依赖注入有关。但是,我开始认为必须有一个更简单的解决方案?

是的,我也想为 Android 做这件事,但首先要做的是。

解决方法

在 App.cs 中创建一个全局变量,在构造函数中初始化。

从公共方法返回值并在iOS项目中访问它。

表单 App.cs

    UITestingHelper helper;
    public string GetAppStatus()
    {
        return helper.UITestingStatus.GetAppStatus;
    }

iOS AppDelegate.cs

  public NSString UITestBackDoor(NSString value)
        {
            return (App.Current as App).GetAppStatus();
        }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...