2017-10-10 16 views
0

Je suis un débutant dans Xamarin.Forms, et j'ai une question: Comment puis-je appeler le code suivant dans mon code partagé/xaml.cs?Comment appeler le code de capture d'écran de dépendance dans mon code partagé?

public class screenshotManager : IScreenshotManager 
{  

    public static Activity Activity { get; set; } 

    public async System.Threading.Tasks.Task<byte[]> CaptureAsync() 
    { 
     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; 
    } 
} 
+1

Avez-vous lu les documents? https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/ – Jason

+0

ive a tenté de placer ce code dans un événement click mais rien ne s'est produit DependencyService.Get (). CaptureAsync(); –

+0

Avez-vous enregistré votre implémentation? '[assembly: Xamarin.Forms.Dependency (typeof (screenshotManager))]' en tant que [demande à docs] (https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/ #Enregistrement)? –

Répondre

0

Si votre besoin est d'obtenir l'image capturée à partir d'un octet [], comme nous avons discuté dans les commentaires, vous devez donc faire comme dit @ Jason.

Quelque chose comme ceci:

var imageBytes = await screenshotManager.CaptureAsync(); 
Image imageView = new Image(); 
imageView.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes));