/// <summary> /// Parse the user interface XAML code. /// </summary> /// <param name="xamlCode">The XAML code to analyze that represents the user interface.</param> /// <returns>Returns a <see cref="FrameworkElement"/> is the parsing succeed. Otherwise, returns null if the XAML code is empty, or an exception if it can't parse it.</returns> private FrameworkElement ParseXaml(string xamlCode) { if (string.IsNullOrWhiteSpace(xamlCode)) { return(null); } FrameworkElement result = null; try { result = SerializationHelper.ConvertFromXaml(xamlCode) as FrameworkElement; } catch (Exception exception) { CoreHelper.ReportException(exception); AddIssue(new BaZicParserException(L.BaZic.Parser.FormattedXamlParsingError(exception.Message))); } if (result == null) { AddIssue(new BaZicParserException(L.BaZic.Parser.XamlUnknownParsingError)); } return(result); }
/// <summary> /// Loads the resource files and adapt the XAML for loading it. /// </summary> private void LoadUserInterface() { var xamlCode = _uiProgram.Xaml; if (_uiProgram.ResourceFilePaths != null) { var xmlns = @"xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"""; var xmlnsx = @"xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"""; var indexOfB = xamlCode.IndexOf(@"xmlns:b="""); if (indexOfB > -1) { var indexOfEndB = xamlCode.IndexOf('\"', xamlCode.IndexOf('\"', indexOfB) + 1); xamlCode = xamlCode.Remove(indexOfB, (indexOfEndB - indexOfB) + 1); } var xmlnsRes = $@"xmlns:b=""clr-namespace:{typeof(RuntimeResourceManager).Namespace};assembly={typeof(RuntimeResourceManager).Assembly.GetName().Name}"""; xamlCode = xamlCode.Replace(xmlns, $"{xmlns} {xmlnsRes}"); if (!xamlCode.Contains(xmlnsx)) { xamlCode = xamlCode.Replace(xmlns, $"{xmlns} {xmlnsx}"); } foreach (var resourceFile in _uiProgram.ResourceFilePaths) { if (File.Exists(resourceFile)) { var fileName = Path.GetFileName(resourceFile); var resourceName = RuntimeResourceManager.AddOrReplaceResource(ExecutionFlowId.ToString(), resourceFile); var xamlResourceFile = resourceFile.Replace("\\", "/"); if (xamlCode.Contains($"file:///{xamlResourceFile}")) { xamlCode = xamlCode.Replace($"file:///{xamlResourceFile}", $"{{Binding Path=[{resourceName}], Source={{x:Static b:{nameof(RuntimeResourceManager)}.{nameof(RuntimeResourceManager.Resources)}}}, Mode=OneWay}}"); } else if (xamlCode.Contains(xamlResourceFile)) { xamlCode = xamlCode.Replace(xamlResourceFile, $"{{Binding Path=[{resourceName}], Source={{x:Static b:{nameof(RuntimeResourceManager)}.{nameof(RuntimeResourceManager.Resources)}}}, Mode=OneWay}}"); } else if (xamlCode.Contains(fileName)) { xamlCode = xamlCode.Replace(fileName, resourceName); } } } } UserInterface = SerializationHelper.ConvertFromXaml(xamlCode) as FrameworkElement; }