获取当前活动 - Xamarin Android

问题描述 投票:0回答:6

我正在为 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。

c# android android-activity xamarin xamarin.android
6个回答
49
投票

更好的方法是使用 Standalone Current Activity PluginXamarin Essentials Plugin 中的 Current Activity Property。然后你可以这样做:

  • 独立:
    CrossCurrentActivity.Current.Activity
  • Xamarin Essentials:
    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
.


32
投票

自 Xamarin 2.5 发布以来,Xamarin.Forms.Forms.Context 已过时。现在可以通过以下方式获取上下文:

var currentContext = Android.App.Application.Context;

21
投票
var activity = (Activity)Forms.Context;

或者如果您正在使用 MainActivity

var activity = (MainActivity)Forms.Context;

19
投票

如果您使用的是 Xamarin Essentials 1.5 或更高版本,那么您可以使用

Platform.CurrentActivity
。这基本上等同于使用 CurrentActivity 插件。

确保按照说明正确初始化它,即。在

MainActivity
OnCreate
添加以下行

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

0
投票

我试图在 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);
        }
    }
}

0
投票
Activity activity = Android.App.Application.Context as Activity;
© www.soinside.com 2019 - 2024. All rights reserved.