private void OnLoaded(object sender, RoutedEventArgs e) { string language = UserSettings.PlayerConfig.HunterPie.Language.Split('\\').LastOrDefault().Replace(".xml", ""); string changelogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Changelog\\changelog-{language}.md"); if (!File.Exists(changelogPath)) { changelogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Changelog\\changelog-en-us.md"); } if (!File.Exists(changelogPath)) { return; } var markdown = File.ReadAllText(changelogPath); var xaml = Markdig.Wpf.Markdown.ToXaml(markdown, BuildPipeline()); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml))) { using (var reader = new XamlXmlReader(stream, new MyXamlSchemaContext())) { if (XamlReader.Load(reader) is FlowDocument document) { Viewer.Document = document; } } } }
//Event handlers /** * Sets the mod description to a markdown formatted document */ private void SetModDetails(object sender, SelectionChangedEventArgs e) { Mod mod = (Mod)((DataGrid)sender).SelectedItem; if (mod.LogoURL == "") { LogoImage.Source = (ImageSource) new ImageSourceConverter().ConvertFrom(Properties.Resources.NotFound); } else { LogoImage.Source = new BitmapImage(new Uri(mod.LogoURL)); } DownloadButton.IsEnabled = true; //Source: https://github.com/Kryptos-FR/markdig.wpf/blob/master/src/Markdig.Xaml.SampleApp/MainWindow.xaml.cs#L36 //Sets the mod details view with the markdown rendered content using MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes( Markdown.ToXaml(mod.FullDescription, new MarkdownPipelineBuilder().UseSupportedExtensions().Build()))); using XamlXmlReader reader = new XamlXmlReader(stream, new XamlSchemaContext()); if (XamlReader.Load(reader) is FlowDocument document) { ModDescription.Document = document; } }
void LoadSkin() { if (ConfigHelper.Main.Values.Debug.UseInternalSkin) { return; } var skinFileName = ConfigHelper.Main.Values.SkinFileName; if (!File.Exists(FileContainer.GetFullPathFileName(skinFileName))) { Log.WriteLine($"Failed to load skin file '{skinFileName}'"); return; } m_SkinFile.TryChangeFileName(skinFileName); try { ResourceDictionary resourceDictionary = null; using (var streamReader = new StreamReader(m_SkinFile.FullPathFileName, Encoding.UTF8)) { LoadExternalAssemblies(); var xmlReaderSettings = new XamlXmlReaderSettings { LocalAssembly = Assembly.GetExecutingAssembly() }; using (var xamlReader = new XamlXmlReader(streamReader.BaseStream, XamlReader.GetWpfSchemaContext(), xmlReaderSettings)) { resourceDictionary = XamlReader.Load(xamlReader) as ResourceDictionary; } } if (resourceDictionary != null) { Resources.MergedDictionaries.Clear(); Resources.MergedDictionaries.Add(resourceDictionary); if (m_Overlay != null) { m_Overlay.RefreshWidgetsLayout(); } Log.WriteLine($"{m_SkinFile.FileName} loaded"); } } catch (Exception ex) { Log.WriteException(ex); } m_LastSkinFileName = skinFileName; }
private void LoadInkCanvas(string inkFileName, List <string> filesName) { var fs = File.Open(inkFileName, FileMode.Open, FileAccess.Read); var savedCanvas = XamlReader.Load(fs) as InkCanvas; fs.Close(); if (savedCanvas != null) { DrawingSheet.Strokes.Add(savedCanvas.Strokes); } filesName.Remove(inkFileName); }
private static ResourceDictionary LoadOrDefault(string path, int trial = 0, XamlObjectWriterException exception = null) { ResourceDictionary resource = null; try { if (!File.Exists(path)) { return(new ResourceDictionary()); } if (exception != null) { var content = File.ReadAllLines(path).ToList(); content.RemoveAt(exception.LineNumber - 1); File.WriteAllLines(path, content); } using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { try { //Read in ResourceDictionary File resource = (ResourceDictionary)XamlReader.Load(fs); } catch (XamlParseException xx) { if (xx.InnerException is XamlObjectWriterException inner && trial < 5) { return(LoadOrDefault(path, trial + 1, inner)); } resource = new ResourceDictionary(); } catch (Exception ex) { //Sets a default value if null. resource = new ResourceDictionary(); } } //Tries to load the resource from disk. //resource = new ResourceDictionary {Source = new Uri(path, UriKind.RelativeOrAbsolute)}; } catch (Exception) { //Sets a default value if null. resource = new ResourceDictionary(); } return(resource); }
private void OnLoaded(object sender, RoutedEventArgs e) { var markdown = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "changelog.log")); var xaml = Markdig.Wpf.Markdown.ToXaml(markdown, BuildPipeline()); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml))) { using (var reader = new XamlXmlReader(stream, new MyXamlSchemaContext())) { if (XamlReader.Load(reader) is FlowDocument document) { Viewer.Document = document; } } } }
private void OnTest(object sender, RoutedEventArgs e) { //object tree = XamlServices.Load("Demo1.xaml"); //container1.Children.Add(tree as UIElement); FileStream stream = File.OpenRead("Demo1.xaml"); object tree = XamlReader.Load(stream); var uiElement = tree as UIElement; if (uiElement != null) { Container1.Children.Add(uiElement); } }
public void loadXamlTest() { var root = Path.GetDirectoryName(typeof(XamlLoadTests).Assembly.Location); var dir = Path.Combine(root, "test_files/xaml"); var context = XamlReader.GetWpfSchemaContext(); foreach (var q in Directory.EnumerateFiles(dir)) { Logger.Info($"Loading XAML file {q}"); var result = XamlReader.Load(new FileStream(q, FileMode.Open)); var xt = context.GetXamlType(result.GetType()); var logXamlType = new XamlTypeLogger(Logger); } }
private void OnLoaded(object sender, RoutedEventArgs e) { var markdown = File.ReadAllText("Documents/Markdig-readme.md"); var xaml = Markdown.ToXaml(markdown, BuildPipeline()); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml))) { var reader = new XamlXmlReader(stream, new MyXamlSchemaContext()); var document = XamlReader.Load(reader) as FlowDocument; if (document != null) { Viewer.Document = document; } } }
public static FlowDocument ToDocument(string markdown) { string?xaml = Markdig.Wpf.Markdown.ToXaml(markdown, new MarkdownPipelineBuilder().UseSupportedExtensions().Build()); using MemoryStream? stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml)); using XamlXmlReader? reader = new XamlXmlReader(stream, new MyXamlSchemaContext()); object?document = XamlReader.Load(reader); if (document is FlowDocument flowDoc) { return(flowDoc); } throw new Exception($"document: {document} was not a flow document"); }
private void ShowMarkdown() { string markdown = _mod.FullDescription; var xaml = Markdig.Wpf.Markdown.ToXaml(markdown, BuildPipeline()); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml))) { using (var reader = new XamlXmlReader(stream, new MyXamlSchemaContext())) { if (XamlReader.Load(reader) is FlowDocument document) { Viewer.Document = document; } } } }
private FlowDocument GenerateDocument(string a_document) { if (string.IsNullOrEmpty(a_document)) { return(null); } var xaml = Markdig.Wpf.Markdown.ToXaml(a_document, BuildPipeline()); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml))) { var reader = new XamlXmlReader(stream, new MyXamlSchemaContext()); var document = XamlReader.Load(reader) as FlowDocument; return(document); } }
static ResultCode ParseLanguages() { var languagePath = GetPath("Languages"); if (languagePath != "" && Directory.Exists(languagePath)) { var langFiles = Directory.GetFiles(languagePath, "*.xaml"); float count = langFiles.Length; var done = 0f; foreach (var langFile in langFiles) { var imageFile = Path.GetFullPath(languagePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(langFile) + ".png"); try { var fs = new FileStream(langFile, FileMode.Open); var dlc = (ResourceDictionary)XamlReader.Load(fs); var language = new Language(dlc, imageFile); if (language.Key == "") { Debug.Log("Configuration", "The language file \"" + langFile + "\" could not be parsed. The LangCode Key is missing.", Debug.Type.Warning); } else { var langKey = language.Key.ToLower(); if (!Languages.ContainsKey(langKey)) { Languages.Add(langKey, language); Debug.Log("Configuration", "Loaded language \"" + language.Key + "\" successfully."); } else { Debug.Log("Configuration", "The language \"" + language.Key + "\" exists more than once.", Debug.Type.Warning); } } } catch (XamlException e) { Debug.Log("Configuration", "The language file \"" + langFile + "\" could not be parsed. Exception: " + e, Debug.Type.Warning); } catch (Exception e) { Debug.Log("Configuration", "The language file \"" + langFile + "\" could not be parsed. Unexpected exception: " + e, Debug.Type.Warning); } done += 1f; ChangeProgress(50f + (done / count) * 40f); } } else { Error = ErrorCode.MalformedConfiguration; ErrorString = "Could not find the languages path."; Debug.Log("Configuration", ErrorString, Debug.Type.Error); return(ResultCode.ERROR); } Debug.Log("Configuration", Languages.Count + " languages parsed successfully."); var ci = CultureInfo.InstalledUICulture; var systemLangKey = ci.TwoLetterISOLanguageName.ToLower(); if (GetString("Language") != "" && Languages.ContainsKey(GetString("Language"))) { ChangeLanguage(GetString("Language")); } else if (Languages.ContainsKey(systemLangKey)) { ChangeLanguage(systemLangKey); } else if (Languages.ContainsKey("en")) { ChangeLanguage("en"); } else if (Languages.Count > 0) { ChangeLanguage(Languages.Keys.ToArray()[0]); } else { Debug.Log("Configuration", "No suitable language found.", Debug.Type.Warning); } return(ResultCode.Ok); }
private static GridViewColumn CreateColumn(GridView gridView, ColumnDescriptor columnSource) { GridViewColumn column = new(); string headerTextMember = GetHeaderTextMember(gridView); string displayMemberMember = GetDisplayMemberMember(gridView); bool checkbox = columnSource.CheckboxMember; string styleString = GetColumnHeadStyleName(gridView); DataTemplate cellStyleTemplate = GetColumnCellTemplate(gridView); column.Width = 90; if (!string.IsNullOrEmpty(headerTextMember)) { column.Header = GetPropertyValue(columnSource, headerTextMember); } if (!string.IsNullOrEmpty(displayMemberMember) && !checkbox) { var propertyName = GetPropertyValue(columnSource, displayMemberMember) as string; var binding = new Binding(propertyName); if (columnSource.OneTime) { binding.Mode = BindingMode.OneTime; } if (cellStyleTemplate != null) { column.CellTemplate = CreateCellTemplate(cellStyleTemplate, binding); } else { column.DisplayMemberBinding = binding; } } else if (checkbox) { var propertyName = GetPropertyValue(columnSource, displayMemberMember) as string; StringReader stringReader = new(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> <CheckBox IsChecked=""{Binding " + propertyName + @"}""/> </DataTemplate>"); XmlReader xmlReader = XmlReader.Create(stringReader); column.CellTemplate = XamlReader.Load(xmlReader) as DataTemplate; column.Width = 24; } if (!string.IsNullOrEmpty(styleString)) { Style style = Application.Current.FindResource(styleString) as Style; if (style != null) { column.HeaderContainerStyle = style; } } if (columnSource.PreferredWidth.HasValue) { column.Width = columnSource.PreferredWidth.Value; } else { // Set Widht to NaN in order to stretch it to match conent column.Width = double.NaN; } return(column); }