c# - Get current Activity - Xamarin Android -
i developing portable app android , ios. current function taking screenshot , use image in code. therefor have interface in portable library.
public interface ifilesystemservice { string getappdatafolder(); }
i taking screenshot in portable library following code:
static public bool takescreenshot() { try { byte[] screenshotbytes = dependencyservice.get<interface.iscreenshotmanager>().takescreenshot(); return true; } catch (exception ex) { } return false; }
this either calls android or ios version.
android:
class screenshotmanagerandroid : iscreenshotmanager { public static activity activity { get; set; } public byte[] takescreenshot() { if (activity == null) { throw new exception("you have set screenshotmanager.activity in 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; }
the question current activity app.
try doing var view = ((activity)xamarin.forms.forms.context).window.decorview;
xamarin automatically assigns activity
forms.context
.
Comments
Post a Comment