コード例 #1
0
        public ICollection <AnnoObject> Paste()
        {
            var files = _clipboard.GetFileDropList();

            if (files?.Count == 1)
            {
                try
                {
                    return(_layoutLoader.LoadLayout(files[0], forceLoad: true).Objects);
                }
                catch (JsonReaderException) { }
            }

            if (_clipboard.ContainsData(CoreConstants.AnnoDesignerClipboardFormat))
            {
                try
                {
                    var stream = _clipboard.GetData(CoreConstants.AnnoDesignerClipboardFormat) as Stream;
                    if (stream is not null)
                    {
                        return(_layoutLoader.LoadLayout(stream, forceLoad: true).Objects);
                    }
                }
                catch (JsonReaderException) { }
            }

            if (_clipboard.ContainsText())
            {
                using var memoryStream = new MemoryStream();
                using var streamWriter = new StreamWriter(memoryStream);
                streamWriter.Write(_clipboard.GetText());
                streamWriter.Flush();
                memoryStream.Seek(0, SeekOrigin.Begin);
                try
                {
                    return(_layoutLoader.LoadLayout(memoryStream, forceLoad: true).Objects);
                }
                catch (JsonReaderException) { }
            }

            return(Array.Empty <AnnoObject>());
        }
コード例 #2
0
        public void Copy_ListHasData_ShouldAddDataToClipboard()
        {
            // Arrange
            var service = GetService();
            var data    = GetListOfObjects();

            // Act
            service.Copy(data);

            // Assert
            var clipboardStream = _mockedClipboard.GetData(CoreConstants.AnnoDesignerClipboardFormat) as System.IO.Stream;
            var copiedObjects   = _mockedLayoutLoader.LoadLayout(clipboardStream, forceLoad: true).Objects;

            Assert.Equal(data.Count, copiedObjects.Count);
            Assert.All(data, x =>
            {
                //Assert.Contains(x, copiedObjects); //not useable because of missing cutom comparer for AnnoObject
                Assert.Contains(copiedObjects, y => y.Identifier.Equals(x.Identifier, StringComparison.OrdinalIgnoreCase));
            });
        }
コード例 #3
0
        private void LoadLayoutFromJson(string jsonString, bool forceLoad = false)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(jsonString))
                {
                    var jsonArray = Encoding.UTF8.GetBytes(jsonString);
                    using (var ms = new MemoryStream(jsonArray))
                    {
                        var loadedLayout = _layoutLoader.LoadLayout(ms, forceLoad);
                        if (loadedLayout != null)
                        {
                            annoCanvas.SelectedObjects.Clear();
                            annoCanvas.PlacedObjects = loadedLayout;
                            annoCanvas.LoadedFile    = string.Empty;
                            annoCanvas.Normalize(1);

                            _mainViewModel.UpdateStatistics();
                        }
                    }
                }
            }
            catch (LayoutFileVersionMismatchException layoutEx)
            {
                Trace.WriteLine(layoutEx);

                if (MessageBox.Show(
                        "Try loading anyway?\nThis is very likely to fail or result in strange things happening.",
                        "File version mismatch", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    LoadLayoutFromJson(jsonString, true);
                }
            }
            catch (Exception e)
            {
                App.WriteToErrorLog("Error loading layout from JSON", e.Message, e.StackTrace);
                MessageBox.Show(e.Message, "Something went wrong while loading the layout.");
            }
        }