首页 > 解决方案 > 获取服务中的活动

问题描述

下面的代码有效。但我知道这不是正确的做事方式,我想知道什么是正确的做法。我有一个在 ListView 中显示 GIF 图像的键盘服务。但是 ListView 需要一个活动来显示它。如何在我的 ListView 中获取活动?(我已经尝试将服务转换为活动。我得到一个无效的转换异常)

  public override View OnCreateInputView()
  {
          View imageKeyboardView = (View)LayoutInflater.Inflate(Resource.Layout.ImageKeyboard, null);
                listView = imageKeyboardView.FindViewById<ListView>(Resource.Id.List); // get reference to the ListView in the layout
                Activity act = MainActivity.Instance;
                // populate the listview with data
                listView.Adapter = new ImageKeyboardAdapter(act, listOfIconUris);
                listView.ItemClick += OnListItemClick;  // to be defined
                return imageKeyboardView;
  }

标签: androidservicexamarin.android

解决方案


您可以尝试使用CurrentActivityPlugin来获取正在显示Application的当前。Activity


推荐阅读