Ryan Linton 7 years ago
parent
commit
adc46b7fd7

+ 2
- 2
README.md View File

95
 
95
 
96
 #### Windows
96
 #### Windows
97
 
97
 
98
-1. In Visual Studio, in the solution explorer, right click on your project solution then select `Add` ➜ `ExisitingProject`
98
+1. In Visual Studio, in the solution explorer, right click on your solution then select `Add` ➜ `ExisitingProject`
99
 2. Go to `node_modules` ➜ `react-native-view-shot` and add `RNViewShot.csproj` (UWP) or optionally `RNViewShot.Net46.csproj` (WPF)
99
 2. Go to `node_modules` ➜ `react-native-view-shot` and add `RNViewShot.csproj` (UWP) or optionally `RNViewShot.Net46.csproj` (WPF)
100
-3. In Visual Studio, in the solution explorer, right click on your project then select `Add` ➜ `Reference`
100
+3. In Visual Studio, in the solution explorer, right click on your Application project then select `Add` ➜ `Reference`
101
 4. Under the projects tab select `RNViewShot` (UWP) or `RNViewShot.Net46` (WPF)
101
 4. Under the projects tab select `RNViewShot` (UWP) or `RNViewShot.Net46` (WPF)
102
 
102
 
103
 
103
 

+ 2
- 1
windows/RNViewShot.Net46/RNViewShotModule.cs View File

51
             }
51
             }
52
 
52
 
53
             UIManagerModule uiManager = this._reactContext.GetNativeModule<UIManagerModule>();
53
             UIManagerModule uiManager = this._reactContext.GetNativeModule<UIManagerModule>();
54
-            uiManager.AddUIBlock(new ViewShot(tag, format, quality, width, height, path, result, promise));
54
+            var viewShot = new ViewShot(tag, format, quality, width, height, path, result, promise);
55
+            uiManager.AddUIBlock(viewShot);
55
         }
56
         }
56
     }
57
     }
57
 }
58
 }

+ 2
- 1
windows/RNViewShot.Net46/ViewShot.cs View File

1
 using ReactNative.Bridge;
1
 using ReactNative.Bridge;
2
 using ReactNative.UIManager;
2
 using ReactNative.UIManager;
3
 using System;
3
 using System;
4
+using System.Diagnostics;
4
 using System.IO;
5
 using System.IO;
5
 using System.Windows;
6
 using System.Windows;
6
 using System.Windows.Media;
7
 using System.Windows.Media;
87
             }
88
             }
88
             catch (Exception ex)
89
             catch (Exception ex)
89
             {
90
             {
90
-                Console.WriteLine(ex.ToString());
91
+                Debug.WriteLine(ex.ToString());
91
                 promise.Reject(ErrorUnableToSnapshot, "Failed to capture view snapshot");
92
                 promise.Reject(ErrorUnableToSnapshot, "Failed to capture view snapshot");
92
             }
93
             }
93
         }
94
         }

+ 2
- 1
windows/RNViewShot/RNViewShotModule.cs View File

51
             }
51
             }
52
 
52
 
53
             UIManagerModule uiManager = this._reactContext.GetNativeModule<UIManagerModule>();
53
             UIManagerModule uiManager = this._reactContext.GetNativeModule<UIManagerModule>();
54
-            uiManager.AddUIBlock(new ViewShot(tag, format, quality, width, height, path, result, promise));
54
+            var viewShot = new ViewShot(tag, format, quality, width, height, path, result, promise);
55
+            uiManager.AddUIBlock(viewShot);
55
         }
56
         }
56
     }
57
     }
57
 }
58
 }

+ 11
- 19
windows/RNViewShot/ViewShot.cs View File

1
 using ReactNative.Bridge;
1
 using ReactNative.Bridge;
2
 using ReactNative.UIManager;
2
 using ReactNative.UIManager;
3
 using System;
3
 using System;
4
+using System.Diagnostics;
4
 using System.IO;
5
 using System.IO;
5
 using System.Runtime.InteropServices.WindowsRuntime;
6
 using System.Runtime.InteropServices.WindowsRuntime;
6
 using System.Threading.Tasks;
7
 using System.Threading.Tasks;
9
 using Windows.Storage;
10
 using Windows.Storage;
10
 using Windows.Storage.Streams;
11
 using Windows.Storage.Streams;
11
 using Windows.UI.Xaml;
12
 using Windows.UI.Xaml;
12
-using Windows.UI.Xaml.Media;
13
 using Windows.UI.Xaml.Media.Imaging;
13
 using Windows.UI.Xaml.Media.Imaging;
14
 
14
 
15
 namespace RNViewShot
15
 namespace RNViewShot
59
             {
59
             {
60
                 if ("file" == result)
60
                 if ("file" == result)
61
                 {
61
                 {
62
-                    using (InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream())
62
+                    using (var ras = new InMemoryRandomAccessStream())
63
                     {
63
                     {
64
                         await CaptureView(view, ras);
64
                         await CaptureView(view, ras);
65
-                        StorageFile file = await GetStorageFile();
65
+                        var file = await GetStorageFile();
66
                         using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
66
                         using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
67
                         {
67
                         {
68
                             await RandomAccessStream.CopyAndCloseAsync(ras.GetInputStreamAt(0), fileStream.GetOutputStreamAt(0));
68
                             await RandomAccessStream.CopyAndCloseAsync(ras.GetInputStreamAt(0), fileStream.GetOutputStreamAt(0));
72
                 }
72
                 }
73
                 else if ("base64" == result)
73
                 else if ("base64" == result)
74
                 {
74
                 {
75
-                    using (InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream())
75
+                    using (var ras = new InMemoryRandomAccessStream())
76
                     {
76
                     {
77
                         await CaptureView(view, ras);
77
                         await CaptureView(view, ras);
78
-                        byte[] imageBytes = new byte[ras.Size];
78
+                        var imageBytes = new byte[ras.Size];
79
                         await ras.AsStream().ReadAsync(imageBytes, 0, imageBytes.Length);
79
                         await ras.AsStream().ReadAsync(imageBytes, 0, imageBytes.Length);
80
                         string data = Convert.ToBase64String(imageBytes);
80
                         string data = Convert.ToBase64String(imageBytes);
81
                         promise.Resolve(data);
81
                         promise.Resolve(data);
83
                 }
83
                 }
84
                 else if ("data-uri" == result)
84
                 else if ("data-uri" == result)
85
                 {
85
                 {
86
-                    using (InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream())
86
+                    using (var ras = new InMemoryRandomAccessStream())
87
                     {
87
                     {
88
                         await CaptureView(view, ras);
88
                         await CaptureView(view, ras);
89
-                        byte[] imageBytes = new byte[ras.Size];
89
+                        var imageBytes = new byte[ras.Size];
90
                         await ras.AsStream().ReadAsync(imageBytes, 0, imageBytes.Length);
90
                         await ras.AsStream().ReadAsync(imageBytes, 0, imageBytes.Length);
91
                         string data = Convert.ToBase64String(imageBytes);
91
                         string data = Convert.ToBase64String(imageBytes);
92
                         data = "data:image/" + extension + ";base64," + data;
92
                         data = "data:image/" + extension + ";base64," + data;
100
             }
100
             }
101
             catch (Exception ex)
101
             catch (Exception ex)
102
             {
102
             {
103
-                Console.WriteLine(ex.ToString());
103
+                Debug.WriteLine(ex.ToString());
104
                 promise.Reject(ErrorUnableToSnapshot, "Failed to capture view snapshot");
104
                 promise.Reject(ErrorUnableToSnapshot, "Failed to capture view snapshot");
105
             }
105
             }
106
         }
106
         }
162
 
162
 
163
         private async Task<StorageFile> GetStorageFile()
163
         private async Task<StorageFile> GetStorageFile()
164
         {
164
         {
165
-            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
166
-            if (string.IsNullOrEmpty(path))
167
-            {
168
-                string fileName = Guid.NewGuid().ToString();
169
-                fileName = Path.ChangeExtension(fileName, extension);
170
-                return await storageFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
171
-            }
172
-            else
173
-            {
174
-                return await storageFolder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting);
175
-            }
165
+            var storageFolder = ApplicationData.Current.LocalFolder;
166
+            var fileName = string.IsNullOrEmpty(path) ? path : Path.ChangeExtension(Guid.NewGuid().ToString(), extension);                
167
+            return await storageFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
176
         }
168
         }
177
     }
169
     }
178
 }
170
 }