Skip to content

Commit

Permalink
Force retry
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebaltazar committed Oct 17, 2024
1 parent b136751 commit 7a09f57
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Maui.ServerDrivenUI/Models/ServerUIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public string ToXaml(IList<CustomNamespace>? customNamespaces = null)
}
}

return XamlConverterService.ConvertToXml(this);
var xaml = XamlConverterService.ConvertToXml(this);
return $"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
$"{xaml}";
}

#endregion
Expand Down
1 change: 0 additions & 1 deletion Maui.ServerDrivenUI/Services/XamlConverterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ internal static class XamlConverterService
public static string ConvertToXml(ServerUIElement element)
{
var strBuilder = new StringBuilder();
strBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
strBuilder.Append($"<{element.Type}");
strBuilder.AppendLine();

Expand Down
13 changes: 10 additions & 3 deletions Maui.ServerDrivenUI/Views/ServerDrivenVisualElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal static async Task InitializeComponentAsync(IServerDrivenVisualElement e
var onLoaded = element.OnLoaded;
var visualElement = (element as VisualElement);
var currentBindingContext = visualElement?.BindingContext;
var forceRetry = false;

try
{
Expand All @@ -51,11 +52,11 @@ internal static async Task InitializeComponentAsync(IServerDrivenVisualElement e
catch (Exception ex)
{
var xamlException = new UnableToLoadXamlException(XAML_LOAD_ERROR_MESSAGE, xaml, ex);
errorMessage = xamlException.Message;
element.OnError(xamlException);
forceRetry = true;
}

if (!IsXamlLoaded(element, attempt))
if (!IsXamlLoaded(element, attempt, forceRetry))
return;

if (visualElement != null)
Expand Down Expand Up @@ -90,8 +91,14 @@ internal static async Task InitializeComponentAsync(IServerDrivenVisualElement e
}
}

private static bool IsXamlLoaded(IServerDrivenVisualElement element, int attempt)
private static bool IsXamlLoaded(IServerDrivenVisualElement element, int attempt, bool forceRetry = false)
{
if (attempt < MAX_RETRIES && forceRetry)
{
_ = Task.Run(() => InitializeComponentAsync(element, attempt++));
return false;
}

switch (element)
{
case ServerDrivenContentPage page when page.Content is null:
Expand Down

0 comments on commit 7a09f57

Please sign in to comment.