【问题标题】:Get current Activity - Xamarin Android获取当前活动 - Xamarin Android
【发布时间】:2017-09-02 22:44:20
【问题描述】:

我正在开发适用于 Android 和 iOS 的便携式应用程序。我当前的功能是截屏并在代码中使用该图像。因此,我在可移植库中有一个接口。

public interface IFileSystemService
{
    string GetAppDataFolder();
}

我也在便携式库中使用以下代码截取屏幕截图:

static public bool TakeScreenshot()
    {
        try
        {
            byte[] ScreenshotBytes = DependencyService.Get<Interface.IScreenshotManager>().TakeScreenshot();
            return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }

这要么调用 Android 版本,要么调用 iOS 版本。

安卓:

class ScreenshotManagerAndroid : IScreenshotManager
{
    public static Activity Activity { get; set; }

    public byte[] TakeScreenshot()
    {

        if (Activity == null)
        {
            throw new Exception("You have to set ScreenshotManager.Activity in your Android project");
        }

        var view = Activity.Window.DecorView;
        view.DrawingCacheEnabled = true;

        Bitmap bitmap = view.GetDrawingCache(true);

        byte[] bitmapData;

        using (var stream = new MemoryStream())
        {
            bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
            bitmapData = stream.ToArray();
        }

        return bitmapData;
    }

现在的问题是从我的应用中获取当前的 Activity。

【问题讨论】:

  • 您需要在 oncreate 方法中为服务分配活动。

标签: c# android android-activity xamarin xamarin.android


【解决方案1】:

更好的方法是使用Standalone Current Activity PluginXamarin Essentials Plugin 中的当前活动属性。然后你可以这样做:

  • 独立:CrossCurrentActivity.Current.Activity
  • Xamarin 要点:Platform.CurrentActivity

如果您不想使用插件并且您的应用中只有 1 个 Activity,您可以在 MainActivity 中分配一个静态变量并在任何需要它的地方引用它,就像这样:

public class MainActivity : FormsApplicationActivity {
    public static Context Context;

    public MainActivity () {
        Context = this;
    }
}

如果您在自定义渲染器中需要Context,您可能希望使用传递给构造函数的Context,如下所示:

public class MyEntryRenderer : EntryRenderer {

    private readonly Context _context;

    public MyEntryRenderer(Context context) : base(context) {
        _context = context;
    }

    // Now use _context or ((Activity)_context) any where you need to (just make sure you pass it into the base constructor)
}

旧的弃用方式是Context view = (Activity)Xamarin.Forms.Forms.Context

Xamarin 自动将Activity 分配给Forms.Context

【讨论】:

  • Xamarin Essentials 现在内置了当前活动插件,请参阅下面的答案。
【解决方案2】:

自 Xamarin 2.5 发布以来,Xamarin.Forms.Forms.Context 为 obsolete。现在可以通过以下方式获取 Context:

var currentContext = Android.App.Application.Context;

【讨论】:

    【解决方案3】:
    var activity = (Activity)Forms.Context;
    

    或者如果你正在使用 MainActivity

    var activity = (MainActivity)Forms.Context;
    

    【讨论】:

    • 是的!我在最初发布我的评论后阅读了你的评论,并意识到我做得过火了。我已经投票了,但它不会显示
    【解决方案4】:

    如果您使用的是 Xamarin Essentials 1.5 或更高版本,则可以使用 Platform.CurrentActivity。这基本上相当于使用 CurrentActivity 插件。

    确保按照说明正确初始化它,即。在MainActivityOnCreate添加以下行

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    

    【讨论】:

    • 如果其他人如果找不到 Platform.CurrentActivity 可能会感到困惑,请确保您使用的是最新的 Xamarin.Essentials。我在 1.3 上看不到它,升级到 1.5 解决了这个问题。
    【解决方案5】:

    我试图在 Xamarin 5 中做类似的事情,并且在我的 Android 和 iOS 版本中使用

    Shell.Current.CurrentPage 
    

    所以会发生一些事情,比如屏幕截图或登录,并且该方法(无论它是什么)可以触发一个静态事件,以便任何感兴趣的活动都可以自行寻找它是否是活动视图,如果是,则消耗数据(字节数组等)由事件传输。

    class FileChooserPage : ContentPage
    {
        public FileChooserPage()
        {
            InitializeComponent();
            GoogleDriveService.Authenticated += GoogleDriveService_Authenticated;
        }
    
        private void GoogleDriveService_Authenticated(object sender, EventArgs e)
        {
            if (ReferenceEquals(this, Shell.Current.CurrentPage))
            {
                Populate(e);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2015-11-29
      • 2014-06-06
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多