Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

431
Views
Obtener actividad actual - Xamarin Android

Estoy desarrollando una aplicación portátil para Android e iOS. Mi función actual es tomar una captura de pantalla y usar esa imagen en el código. Por lo tanto, tengo una interfaz en la biblioteca portátil.

 public interface IFileSystemService { string GetAppDataFolder(); }

Estoy tomando la captura de pantalla también en la biblioteca portátil con el siguiente código:

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

Esto llama a la versión de Android o iOS.

Androide:

 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; }

La pregunta ahora es obtener la actividad actual de mi aplicación.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Una mejor manera sería usar el complemento de actividad actual independiente o la propiedad de actividad actual en el complemento de Xamarin Essentials . Entonces podrías simplemente hacer:

  • Independiente: CrossCurrentActivity.Current.Activity
  • Fundamentos de Xamarin: Platform.CurrentActivity

Si no desea usar un complemento y solo tiene 1 Activity en su aplicación, puede salirse con la suya asignando una variable estática en MainActivity y haciendo referencia a eso donde sea que lo necesite de esta manera:

 public class MainActivity : FormsApplicationActivity { public static Context Context; public MainActivity () { Context = this; } }

Si necesita Context dentro de un renderizador personalizado, querrá usar el Context pasado al constructor, así:

 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) }

La antigua forma en desuso sería Context view = (Activity)Xamarin.Forms.Forms.Context

Xamarin asigna automáticamente la Activity a Forms.Context .

about 4 years ago · Santiago Trujillo Report

0

Desde el lanzamiento de Xamarin 2.5, Xamarin.Forms.Forms.Context está obsoleto . El contexto ahora se puede obtener de la siguiente manera:

 var currentContext = Android.App.Application.Context;
about 4 years ago · Santiago Trujillo Report

0

var activity = (Activity)Forms.Context;

o si está utilizando MainActivity

 var activity = (MainActivity)Forms.Context;
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!