public void Share(Forms9Patch.MimeItemCollection mimeItemCollection, VisualElement target) { var nsItemProviders = mimeItemCollection.AsNSItemProviders(); var activityController = new UIActivityViewController(nsItemProviders.ToArray(), null); var window = UIApplication.SharedApplication.KeyWindow; var vc = window.RootViewController; while (vc.PresentedViewController != null) { vc = vc.PresentedViewController; } if (Xamarin.Forms.Device.Idiom == TargetIdiom.Tablet) { var targetUIView = Xamarin.Forms.Platform.iOS.Platform.GetRenderer(target).NativeView; activityController.PopoverPresentationController.SourceView = targetUIView; activityController.PopoverPresentationController.SourceRect = new CGRect(new CGPoint(0, 0), targetUIView.Frame.Size); // targetUIView.Frame; //activityController.PopoverPresentationController.BackgroundColor = UIColor.DarkGray; // sets the popup's background color activityController.PopoverPresentationController.PageOverlayView().BackgroundColor = UIColor.Black.ColorWithAlpha(0.4f); } vc.PresentViewController(activityController, true, null); }
public void Share(Forms9Patch.MimeItemCollection mimeItemCollection, VisualElement target) { var uris = mimeItemCollection.AsContentUris(); if (uris.Count > 0) { Intent intent = new Intent(); string html = null; string text = null; foreach (var item in ClipboardContentProvider.UriItems) { if (html == null && item.Value.MimeType == "text/html") { html = (string)item.Value.Value; break; } if (text == null && item.Value.MimeType == "text/plain") { text = (string)item.Value.Value; } } if (html != null) { intent.PutExtra(Intent.ExtraText, text ?? html); intent.PutExtra(Intent.ExtraHtmlText, html); } else if (text != null) { intent.PutExtra(Intent.ExtraText, text); } if (uris.Count == 1) { intent.SetAction(Intent.ActionSend); intent.PutExtra(Intent.ExtraStream, uris[0]); intent.AddFlags(ActivityFlags.GrantReadUriPermission); intent.SetType(ClipboardContentProvider.UriItems[uris[0]].MimeType); } else { intent.SetAction(Intent.ActionSendMultiple); intent.PutParcelableArrayListExtra(Intent.ExtraStream, uris.ToArray()); intent.AddFlags(ActivityFlags.GrantReadUriPermission); intent.SetType(mimeItemCollection.LowestCommonMimeType()); } try { Forms9Patch.Droid.Settings.Activity.StartActivity(Intent.CreateChooser(intent, "Share ...")); } catch (Exception e) { using (Forms9Patch.Toast.Create("Sharing Failure", e.Message)) { } } } }
public void Share(Forms9Patch.MimeItemCollection mimeItemCollection, VisualElement target) { _mimeItemCollection = mimeItemCollection; if (_mimeItemCollection.Items.Count > 0) { DataTransferManager.ShowShareUI(); } }
/// <summary> /// Adds the content from file (as byte[]) to collection. Alternative to adding file as FileInfo as the Value of a MimeItem. /// </summary> /// <returns>The bytes from file.</returns> /// <param name="mimeItemCollection">MIME item collection.</param> /// <param name="mimeType">MIME type.</param> /// <param name="path">File Path.</param> public static byte[] AddBytesFromFile(this Forms9Patch.MimeItemCollection mimeItemCollection, string mimeType, string path) { if (File.ReadAllBytes(path) is byte[] byteArray) { mimeItemCollection.Items.Add(new MimeItem <byte[]>(mimeType, byteArray)); return(byteArray); } return(null); }
/// <summary> /// Adds the content from file (as byte[]) to collection. Alternative to adding file as FileInfo as the Value of a MimeItem. /// </summary> /// <returns>The bytes from file.</returns> /// <param name="mimeItemCollection">MIME item collection.</param> /// <param name="mimeType">MIME type.</param> /// <param name="path">File Path.</param> public static byte[] AddBytesFromFile(this Forms9Patch.MimeItemCollection mimeItemCollection, string mimeType, string path) { if (File.ReadAllBytes(path) is byte[] byteArray && byteArray.Length > 0) { mimeItemCollection.Items.Add(new MimeItem <byte[]>(mimeType, byteArray)); return(byteArray); } using (Alert.Create(null, "Cannot access empty file [" + path + "]")) { } return(null); }
/// <summary> /// Adds the content from file (as byte[]) to collection. Alternative to adding file as FileInfo as the Value of a MimeItem. /// </summary> /// <returns>The bytes from file.</returns> /// <param name="mimeItemCollection">MIME item collection.</param> /// <param name="mimeType">MIME type.</param> /// <param name="path">File Path.</param> /// <param name="failAction">What to do if the method fails</param> public static byte[] AddBytesFromFile(this Forms9Patch.MimeItemCollection mimeItemCollection, string mimeType, string path, FailAction failAction = FailAction.ShowAlert) { if (File.ReadAllBytes(path) is byte[] byteArray && byteArray.Length > 0) { mimeItemCollection.Items.Add(new MimeItem <byte[]>(mimeType, byteArray)); return(byteArray); } if (failAction == FailAction.ShowAlert) { using (Alert.Create(null, "Cannot access empty file [" + path + "]")) { } } else if (failAction == FailAction.ThrowException) { throw new System.Exception("Cannot access empty file [" + path + "]"); } return(null); }
/// <summary> /// Adds the content from file (as byte[]) to collection. Alternative to adding file as FileInfo as the Value of a MimeItem. /// </summary> /// <returns>The bytes from file.</returns> /// <param name="mimeItemCollection">MIME item collection.</param> /// <param name="mimeType">MIME type.</param> /// <param name="path">File Path.</param> /// <param name="failAction">What to do if the method fails</param> public static byte[] AddBytesFromFile(this Forms9Patch.MimeItemCollection mimeItemCollection, string mimeType, string path, FailAction failAction = FailAction.ShowAlert) { if (File.ReadAllBytes(path) is byte[] byteArray && byteArray.Length > 0) { mimeItemCollection.Items.Add(new MimeItem <byte[]>(mimeType, byteArray)); return(byteArray); } if (failAction == FailAction.ShowAlert) { Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(async() => { using (var toast = Alert.Create(null, "Cannot access empty file [" + path + "]")) { await toast.WaitForPoppedAsync(); } }); } else if (failAction == FailAction.ThrowException) { throw new System.Exception("Cannot access empty file [" + path + "]"); } return(null); }
public WebViewExport() { Forms9Patch.WebViewPrintEffect.ApplyTo(webView); webView.Source = "https://xamarin.com"; layout.Children.Add(segmentedControl); layout.Children.Add(webView, 0, 1); Padding = 10; Content = layout; segmentedControl.SegmentTapped += async(sender, e) => { e.Segment.IsSelected = false; Forms9Patch.ToFileResult result = null; if (e.Segment.Text == "ToPng") { if (await Forms9Patch.ToPngService.ToPngAsync(webView, "test") is Forms9Patch.ToFileResult fileResult) { result = fileResult; } /* * { * using (Forms9Patch.Alert.Create(e.Segment.Text + (fileResult.IsError ? " Error" : " Success"), fileResult.Result)) { } * var entry = new Forms9Patch.MimeItemCollection(); * Forms9Patch.IMimeItemCollectionExtensions.AddBytesFromFile(entry, "image/png", fileResult.Result); * Forms9Patch.Sharing.Share(entry, segmentedControl.Segments[0]); * * } */ } else if (e.Segment.Text == "ToPdf Letter") { if (Device.RuntimePlatform == Device.UWP) { using (Forms9Patch.Toast.Create("ToPdf not available in UWP.", "Use Forms9Patch.PrintService.PrintAsync instead")) { } } else if (await Forms9Patch.ToPdfService.ToPdfAsync(webView, "test", Forms9Patch.PageSize.NaLetter) is Forms9Patch.ToFileResult fileResult) { result = fileResult; } } else if (e.Segment.Text == "ToPdf A4") { if (Device.RuntimePlatform == Device.UWP) { using (Forms9Patch.Toast.Create("ToPdf not available in UWP.", "Use Forms9Patch.PrintService.PrintAsync instead")) { } } else if (await Forms9Patch.ToPdfService.ToPdfAsync(webView, "test", Forms9Patch.PageSize.IsoA4) is Forms9Patch.ToFileResult fileResult) { result = fileResult; } } else if (e.Segment.Text == "Print") { if (Forms9Patch.PrintService.CanPrint) { await Forms9Patch.PrintService.PrintAsync(webView, "Forms9Patch WebView Print"); } else { using (Forms9Patch.Alert.Create(e.Segment.Text, "Print not available on this device")) { } } } if (result != null) { if (result.IsError) { using (Forms9Patch.Alert.Create(e.Segment.Text + " Error", result.Result)) { } } else { var info = new System.IO.FileInfo(result.Result); Forms9Patch.Toast.Create("Export file", result.Result + "\n size: " + info.Length); var entry = new Forms9Patch.MimeItemCollection(); Forms9Patch.IMimeItemCollectionExtensions.AddBytesFromFile(entry, e.Segment.Text == "ToPng" ? "image/png" : "application/pdf", result.Result); Forms9Patch.Sharing.Share(entry, e.Segment); /* * await Xamarin.Essentials.Share.RequestAsync(new Xamarin.Essentials.ShareFileRequest * { * Title = e.Segment.Text, * File = new Xamarin.Essentials.ShareFile(result.Result) * }); */ } } }; }
public void Share(Forms9Patch.MimeItemCollection mimeItemCollection, VisualElement target) => Share(mimeItemCollection, target, false);