Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/XamlCompiler/BuildTasks/CompileXamlInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3087,9 +3087,19 @@ internal void LogError_XamlInternalError(Exception e, string file)

internal void LogUnhandledException(string subcategory, ErrorCode code, Exception e, string file)
{
XamlException xamlException = e as XamlException;
int line = xamlException != null ? xamlException.LineNumber : 0;
int column = xamlException != null ? xamlException.LinePosition : 0;
int line = 0;
int column = 0;
if (e is XamlException xamlException)
{
line = xamlException.LineNumber;
column = xamlException.LinePosition;
}
else if (e is XmlException xmlException)
{
// XmlException does not derive from XamlException, so extract its position separately.
line = xmlException.LineNumber;
column = xmlException.LinePosition;
}
string message = e.Message;

LogError(subcategory, code, null, file, line, column, 0, 0, message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.IO;
using System.Xaml;
using System.Xml;
Expand All @@ -9,6 +10,22 @@ namespace Microsoft.UI.Xaml.Markup.Compiler.Utilities
{
class XamlNodeStreamHelper
{
// Speculative x:Class read. Malformed markup is reported by the authoritative parse in
// CompileXamlInternal.LoadXamlDom, which names the offending file, so it must not fail here first.
public static bool TryReadXClassFromXamlFileStream(TextReader fileStream, XamlSchemaContext schemaContext, out string className)
{
try
{
className = ReadXClassFromXamlFileStream(fileStream, schemaContext);
return true;
}
catch (Exception e) when (e is XmlException || e is XamlException)
{
className = null;
return false;
}
}

public static string ReadXClassFromXamlFileStream(TextReader fileStream, XamlSchemaContext schemaContext)
{
string className = null;
Expand Down
5 changes: 3 additions & 2 deletions src/XamlCompiler/BuildTasks/SourceFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ public void PropagateOutOfDateStatus(DirectUI.DirectUISchemaContext context)
// look to see if a new class was specified; if so, we guessed wrong before, undo that.
// Note that both the old and new classes are out of date.
string newClassFullName = null;
bool readClassName;
using (var fileReader = TaskFileService.GetFileContents(tif.SourceXamlFullPath))
{
newClassFullName = XamlNodeStreamHelper.ReadXClassFromXamlFileStream(fileReader, context);
readClassName = XamlNodeStreamHelper.TryReadXClassFromXamlFileStream(fileReader, context, out newClassFullName);
}

if (newClassFullName != tif.ClassFullName)
if (readClassName && newClassFullName != tif.ClassFullName)
{
UnregisterClassOfTaskItem(tif);
tif.ClassFullName = newClassFullName;
Expand Down
Loading