:) Я пытаюсь внедрить сервис скриншотов, который отвечает за создание скриншота страницы и размещение его на платформах социальных сетей, которые пользователь установил или установил.Как стандартная панель инструментов.Я мог бы только поделиться изображениями с вашего сайта, так как ниже приведен код, в котором я застрял:
public class ShareImage : Activity, IShareImage
{
public Task Share(string screenshotPath)
{
var path = Android.OS.Environment.GetExternalStoragePublicDirectory("Temp");
if (!File.Exists(path.Path))
{
Directory.CreateDirectory(path.Path);
}
string absPath = path.Path + "tempfile.jpg";
File.WriteAllBytes(absPath, GetBytes(screenshotPath));
var _context = Android.App.Application.Context;
Intent sendIntent = new Intent(global::Android.Content.Intent.ActionSend);
sendIntent.PutExtra(global::Android.Content.Intent.ExtraText, "Application Name");
sendIntent.SetType("image/*");
sendIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse("file://" + absPath));
_context.StartActivity(Intent.CreateChooser(sendIntent, "Sharing"));
return Task.FromResult(0);
}
public static byte[] GetBytes(string screenshotPath)
{
//var stream = new MemoryStream(await CrossScreenshot.Current.CaptureAsync());
byte[] bytes;
bytes = DependencyService.Get<IScreenshotService>().CaptureScreenAsync();
return bytes;
}
}
Я добавил сервис скриншотов для текущей деятельности, которым я и хочу поделиться.
public class ScreenshotService : IScreenshotService
{
private Activity _currentActivity;
public void SetActivity(Activity activity)
{
_currentActivity = activity;
}
public byte[] Caputre()
{
var rootView = _currentActivity.Window.DecorView.RootView;
using (var screenshot = Bitmap.CreateBitmap(
rootView.Width,
rootView.Height,
Bitmap.Config.Argb8888))
{
var canvas = new Canvas(screenshot);
rootView.Draw(canvas);
using (var stream = new MemoryStream())
{
screenshot.Compress(Bitmap.CompressFormat.Png, 90, stream);
return stream.ToArray();
}
}
}
}
С этими двумя службами у меня возникла проблема, заключающаяся в том, что я делился не строкой URL, а моим текущим видом страницы.Приложение выдает исключение
Object reference not set to an instance of an object.
Моя модель представления
public class MainViewModel : INotifyPropertyChanged
{
private readonly INavigation navigation;
public MainViewModel()
{
//TakeScreenshotCommand = new Command(CaptureScreenshot);
//sample data
SampleImageUrl = "minusikona.png";
ShareImageCommand = new Command(() => SharingImage());
}
private async void SharingImage()
{
try
{
await DependencyService.Get<IShareImage>().Share(ScreenshotImage);
}
catch(Exception ex)
{
Console.Write(ex);
await App.Current.MainPage.DisplayAlert("Eror", "Something went wrong! ;(. Please try again later! :) ", "OK");
}
}
public string SampleImageUrl { get; set; }
public ICommand TakeScreenshotCommand { get; set; }
public ICommand ShareImageCommand { get; set; }
private string _screenshotImage;
public string ScreenshotImage
{
get => _screenshotImage;
set
{
if (_screenshotImage != value)
{
_screenshotImage = value;
OnPropertyChanged("_screenshotImage");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Я не могу понять это.Для любой помощи, как продвинуть это дальше, я буду очень полезным.