首页 > 解决方案 > 依赖服务和在机器人和标准项目之间传递值

问题描述

我有一个需要使用本机 droid 函数的方法。我正在使用依赖服务来实现这很好,但是我还需要发送一个填充到我的标准项目中的值。调试时我看到标准中的值但是一旦我进入 droid 值是 null 我也尝试过使列表静态但无济于事

我的服务

 public interface INavigationService
{
  void PushDictionary(List<Word> allWordsOfUserForAutomat);
}

我的实现

public class NavigationImplementation : Activities.INavigationService
 {
            public void PushDictionary(List<Word> allWordsOfUserForAutomat)  //HERE I SEE THE VALUE
          {
              Intent intent = new Intent(MainActivity.Instance,typeof(LockScreenDictionary));
            MainActivity.Instance.StartActivity(intent);
              
            }
}

我的标准

protected void LockScreen()
  {
                    
     if (!viewDisabled)
       {
         DependencyService.Get<INavigationService>().PushDictionary(_allWordsOfUserForAutomat); //HERE I SEE THE VALUE
                     
        }
         else
        {
    NotificationService.ShowToast("Nothing to play");
        }
     }

我的机器人项目

[Activity(Label = "LockScreenDictionary", Theme = "@style/Theme.Splash")]

 public class LockScreenDictionary : FormsAppCompatActivity
     {
    
      private List<Word> _allWordsOfUserForAutomat;  //HERE ITS NULL
      protected override void OnCreate(Bundle savedInstanceState)
      {
        base.OnCreate(savedInstanceState);
       LangUpDictionaryPlayer.PlayAutomat(_allWordsOfUserForAutomat);  //HERE ITS NULL
       }
 }

标签: xamarin.forms

解决方案


您应该将allWordsOfUserForAutomatto Intent 传递给:

在您的实施中:

public class NavigationImplementation : INavigationService
{
    public void PushDictionary(List<Word> allWordsOfUserForAutomat)  //HERE I SEE THE VALUE
    {
        Intent intent = new Intent(MainActivity.Instance, typeof(LockScreenDictionary));
        //pass data
        intent.PutExtra("myData", allWordsOfUserForAutomat); 
        MainActivity.Instance.StartActivity(intent);
    }
}

在您的机器人项目中:

public class LockScreenDictionary : FormsAppCompatActivity
{

    private List<Word> _allWordsOfUserForAutomat;  //HERE ITS NULL
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        _allWordsOfUserForAutomat = Intent.Extras.GetInt("myData");

        LangUpDictionaryPlayer.PlayAutomat(_allWordsOfUserForAutomat);  //HERE ITS NULL
    }
}

推荐阅读