コード例 #1
0
        private async Task SetEditorValue()
        {
            var largeTextValue  = _largeValue as ILargeTextValue;
            var collectionValue = _largeValue as ICollectionValue;
            var complexType     = _largeValue as IComplexType;

            try
            {
                if (largeTextValue != null)
                {
                    TabText.Visibility = Visibility.Visible;
                    TabText.IsSelected = true;

                    _isXml = true;
                    using (var reader = new XmlTextReader(new StringReader(largeTextValue.Value)))
                    {
                        try
                        {
                            while (reader.Read())
                            {
                            }
                        }
                        catch
                        {
                            _isXml = false;
                        }
                    }

                    _isJson = true;

                    try
                    {
                        JToken.Parse(largeTextValue.Value);
                    }
                    catch (Exception)
                    {
                        _isJson = false;
                    }

                    TextEditor.Text = largeTextValue.Value;

                    if (_isXml)
                    {
                        TextEditor.SyntaxHighlighting = XmlHighlightingDefinition;
                    }
                    else if (_isJson)
                    {
                        TextEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition(SyntaxHighlightingNameJavaScript);
                    }

                    TabRaw.Visibility = Visibility.Visible;
                    await DisplayRawData(Encoding.UTF8.GetBytes(largeTextValue.Value));
                }
                else if (_largeBinaryValue != null)
                {
                    TabRaw.Visibility = Visibility.Visible;
                    TabRaw.IsSelected = true;
                    TabRaw.Header     = $"Raw ({_largeBinaryValue.Length:N0} bytes)";

                    await DisplayRawData(_largeBinaryValue.Value);

                    ButtonSaveRawAs.Visibility = Visibility.Visible;
                }
                else if (collectionValue != null)
                {
                    TabCollection.Visibility = Visibility.Visible;
                    TabCollection.IsSelected = true;
                    var columnTemplate = DataGridHelper.CreateDataGridTemplateColumn(collectionValue.ColumnHeader);
                    CollectionViewer.Columns.Add(columnTemplate);
                    CollectionViewer.ItemsSource = collectionValue.Records.Cast <object>().Select(r => new [] { r }).ToArray();
                }
                else if (complexType != null)
                {
                    TabComplexType.Visibility     = Visibility.Visible;
                    TabComplexType.IsSelected     = true;
                    ComplexTypeViewer.ComplexType = complexType;
                }
            }
            catch (OperationCanceledException)
            {
                TraceLog.WriteLine("User has canceled large data editor load operation. ");
                Close();
            }
            catch (Exception e)
            {
                Messages.ShowError(e.Message, owner: this);
                Close();
            }
        }