Skip to content

Commit 63cd106

Browse files
Merge pull request #2 from MuthukumarMadasamy/SampleCorrections
Ticket 802661 - Updating the sample to .NET 10.
2 parents 1188380 + 3037755 commit 63cd106

8 files changed

Lines changed: 381 additions & 132 deletions

File tree

‎SfDataGridSample/MainPage.xaml.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using SfDataGridSample.Services;
2-
using Syncfusion.Maui.DataGrid;
1+
using Syncfusion.Maui.DataGrid;
32
using Syncfusion.Maui.DataGrid.Exporting;
43
using Syncfusion.Maui.DataGrid.Helper;
54
using Syncfusion.Pdf.Graphics;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Android.Content;
2+
using Android.OS;
3+
using Java.IO;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace SfDataGridSample;
9+
10+
public partial class SaveService
11+
{
12+
public partial void SaveAndView(string filename, string contentType, MemoryStream stream)
13+
{
14+
string exception = string.Empty;
15+
string? root = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
16+
17+
Java.IO.File myDir = new(root + "/Syncfusion");
18+
myDir.Mkdir();
19+
20+
Java.IO.File file = new(myDir, filename);
21+
22+
if (file.Exists())
23+
{
24+
file.Delete();
25+
}
26+
27+
try
28+
{
29+
FileOutputStream outs = new(file);
30+
outs.Write(stream.ToArray());
31+
32+
outs.Flush();
33+
outs.Close();
34+
}
35+
catch (Exception e)
36+
{
37+
exception = e.ToString();
38+
}
39+
if (file.Exists())
40+
{
41+
42+
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.N)
43+
{
44+
var fileUri = AndroidX.Core.Content.FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.PackageName + ".provider", file);
45+
var intent = new Intent(Intent.ActionView);
46+
intent.SetData(fileUri);
47+
intent.AddFlags(ActivityFlags.NewTask);
48+
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
49+
Android.App.Application.Context.StartActivity(intent);
50+
}
51+
else
52+
{
53+
var fileUri = Android.Net.Uri.Parse(file.AbsolutePath);
54+
var intent = new Intent(Intent.ActionView);
55+
intent.SetDataAndType(fileUri, contentType);
56+
intent = Intent.CreateChooser(intent, "Open File");
57+
intent!.AddFlags(ActivityFlags.NewTask);
58+
Android.App.Application.Context.StartActivity(intent);
59+
}
60+
61+
}
62+
}
63+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using Foundation;
2+
using QuickLook;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using UIKit;
7+
8+
namespace SfDataGridSample;
9+
10+
public partial class SaveService
11+
{
12+
public partial void SaveAndView(string filename, string contentType, MemoryStream stream)
13+
{
14+
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
15+
string filePath = Path.Combine(path, filename);
16+
stream.Position = 0;
17+
//Saves the document
18+
using FileStream fileStream = new(filePath, FileMode.Create, FileAccess.ReadWrite);
19+
stream.CopyTo(fileStream);
20+
fileStream.Flush();
21+
fileStream.Dispose();
22+
//Launch the file
23+
//Added this code to resolve the warning thrown when CI compiles.
24+
UIViewController? currentController = UIApplication.SharedApplication.ConnectedScenes.OfType<UIWindowScene>().SelectMany(scene => scene.Windows).FirstOrDefault(window => window.IsKeyWindow)!.RootViewController;
25+
while (currentController!.PresentedViewController != null)
26+
currentController = currentController.PresentedViewController;
27+
UIView? currentView = currentController.View;
28+
29+
QLPreviewController qlPreview = new();
30+
QLPreviewItem item = new QLPreviewItemBundle(filename, filePath);
31+
qlPreview.DataSource = new PreviewControllerDS(item);
32+
currentController.PresentViewController((UIViewController)qlPreview, true, null);
33+
}
34+
}
35+
36+
public class QLPreviewItemFileSystem : QLPreviewItem
37+
{
38+
readonly string _fileName, _filePath;
39+
40+
public QLPreviewItemFileSystem(string fileName, string filePath)
41+
{
42+
_fileName = fileName;
43+
_filePath = filePath;
44+
}
45+
46+
public override string PreviewItemTitle
47+
{
48+
get
49+
{
50+
return _fileName;
51+
}
52+
}
53+
public override NSUrl PreviewItemUrl
54+
{
55+
get
56+
{
57+
return NSUrl.FromFilename(_filePath);
58+
}
59+
}
60+
}
61+
62+
public class QLPreviewItemBundle : QLPreviewItem
63+
{
64+
readonly string _fileName, _filePath;
65+
public QLPreviewItemBundle(string fileName, string filePath)
66+
{
67+
_fileName = fileName;
68+
_filePath = filePath;
69+
}
70+
71+
public override string PreviewItemTitle
72+
{
73+
get
74+
{
75+
return _fileName;
76+
}
77+
}
78+
public override NSUrl PreviewItemUrl
79+
{
80+
get
81+
{
82+
var documents = NSBundle.MainBundle.BundlePath;
83+
var lib = Path.Combine(documents, _filePath);
84+
var url = NSUrl.FromFilename(lib);
85+
return url;
86+
}
87+
}
88+
}
89+
90+
public class PreviewControllerDS : QLPreviewControllerDataSource
91+
{
92+
private readonly QLPreviewItem _item;
93+
94+
public PreviewControllerDS(QLPreviewItem item)
95+
{
96+
_item = item;
97+
}
98+
99+
public override nint PreviewItemCount(QLPreviewController controller)
100+
{
101+
return (nint)1;
102+
}
103+
104+
public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
105+
{
106+
return _item;
107+
}
108+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Windows.Storage;
5+
using Windows.Storage.Pickers;
6+
using Windows.Storage.Streams;
7+
using Windows.UI.Popups;
8+
9+
namespace SfDataGridSample;
10+
11+
public partial class SaveService
12+
{
13+
public async partial void SaveAndView(string filename, string contentType, MemoryStream stream)
14+
{
15+
StorageFile stFile;
16+
string extension = Path.GetExtension(filename);
17+
//Gets process windows handle to open the dialog in application process.
18+
IntPtr windowHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
19+
if (!Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
20+
{
21+
//Creates file save picker to save a file.
22+
FileSavePicker savePicker = new();
23+
if (extension == ".xlsx")
24+
{
25+
savePicker.DefaultFileExtension = ".xlsx";
26+
savePicker.SuggestedFileName = filename;
27+
//Saves the file as xlsx file.
28+
savePicker.FileTypeChoices.Add("XLSX", new List<string>() { ".xlsx" });
29+
}
30+
else if(extension == ".pdf")
31+
{
32+
savePicker.DefaultFileExtension = ".pdf";
33+
savePicker.SuggestedFileName = filename;
34+
35+
//Saves the file as Pdf file.
36+
savePicker.FileTypeChoices.Add("PDF", new List<string>() { ".pdf" });
37+
}
38+
39+
WinRT.Interop.InitializeWithWindow.Initialize(savePicker, windowHandle);
40+
stFile = await savePicker.PickSaveFileAsync();
41+
}
42+
else
43+
{
44+
StorageFolder local = ApplicationData.Current.LocalFolder;
45+
stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
46+
}
47+
if (stFile != null)
48+
{
49+
using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
50+
{
51+
//Writes compressed data from memory to file.
52+
using Stream outstream = zipStream.AsStreamForWrite();
53+
outstream.SetLength(0);
54+
//Saves the stream as file.
55+
byte[] buffer = stream.ToArray();
56+
outstream.Write(buffer, 0, buffer.Length);
57+
outstream.Flush();
58+
}
59+
//Create message dialog box.
60+
MessageDialog msgDialog = new("Do you want to view the document?", "File has been created successfully");
61+
UICommand yesCmd = new("Yes");
62+
msgDialog.Commands.Add(yesCmd);
63+
UICommand noCmd = new("No");
64+
msgDialog.Commands.Add(noCmd);
65+
66+
WinRT.Interop.InitializeWithWindow.Initialize(msgDialog, windowHandle);
67+
68+
//Showing a dialog box.
69+
IUICommand cmd = await msgDialog.ShowAsync();
70+
if (cmd.Label == yesCmd.Label)
71+
{
72+
//Launch the saved file.
73+
await Windows.System.Launcher.LaunchFileAsync(stFile);
74+
}
75+
}
76+
}
77+
}

‎SfDataGridSample/Platforms/Windows/SaveWindows.cs‎

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)