获取最新活动 - Xamarin Android

Lex*_*exu 16 c# android xamarin.android android-activity xamarin

我正在为Android和iOS开发便携式应用程序.我当前的功能是截取屏幕截图并在代码中使用该图像.因此我在便携式库中有一个接口.

public interface IFileSystemService
{
    string GetAppDataFolder();
}
Run Code Online (Sandbox Code Playgroud)

我正在使用以下代码在可移植库中获取屏幕截图:

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

这可以调用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;
    }
Run Code Online (Sandbox Code Playgroud)

现在的问题是从我的应用程序获取当前活动.

hva*_*an3 27

更好的方法是使用Current Activity Plugin.那你就可以做CrossCurrentActivity.Current.Activity.

如果您不想使用插件并且Activity在应用程序中只有1个,那么您可以放弃分配静态变量MainActivity并在需要的地方引用它,如下所示:

public class MainActivity : FormsApplicationActivity {
    public static Context Context;

    public MainActivity () {
        Context = this;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您需要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)
}
Run Code Online (Sandbox Code Playgroud)

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

Xamarin自动分配ActivityForms.Context.

  • Xamarin Essentials 现在内置了当前活动插件,请参阅下面的答案。 (2认同)

Wil*_*ner 16

var activity = (Activity)Forms.Context;
Run Code Online (Sandbox Code Playgroud)

或者如果您使用的是MainActivity

var activity = (MainActivity)Forms.Context;
Run Code Online (Sandbox Code Playgroud)


Jam*_*ate 15

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

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

Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Run Code Online (Sandbox Code Playgroud)

  • 对于其他可能因找不到“Platform.CurrentActivity”而感到困惑的人,请确保您使用的是最新的 Xamarin.Essentials。我使用的是 1.3,看不到它,升级到 1.5 解决了这个问题。 (4认同)

Hag*_*ard 13

自Xamarin 2.5发布以来,Xamarin.Forms.Forms.Context已经过时.现在可以按如下方式获得上下文:

var currentContext = Android.App.Application.Context;
Run Code Online (Sandbox Code Playgroud)