kuiperzone / AvantGarde

Avalonia XAML Preview for Linux and Windows
GNU General Public License v3.0
228 stars 10 forks source link

Preview fails with bound dimensions #28

Closed iq2luc closed 1 year ago

iq2luc commented 1 year ago

Subject

Preview fails if the dimensions of the control cannot be directly obtain from the XML by converting the attribute value to a number (e.g. when the value is bound to a data model variable).

Details

If the preview-able control has Width / MinWidth / MaxWidth and / or Height / MinHeight / MaxHeight bound to a data model variable, not just simple numbers in the XML, the preview fails with the error The input string '{Binding Path=ViewWidth}' was not in a correct format.

Screenshot

preview-fails-with-dimension-bound

Proposed solution (workaround?)

The following patch fixes the issue for me:

diff --git a/AvantGarde/Loading/PreviewFactory.cs b/AvantGarde/Loading/PreviewFactory.cs
index 898068a..afdbeb7 100644
--- a/AvantGarde/Loading/PreviewFactory.cs
+++ b/AvantGarde/Loading/PreviewFactory.cs
@@ -73,8 +73,16 @@ namespace AvantGarde.Loading
                             var root = doc.Root ?? throw new XmlException("No root element");
                             _source.DesignWidth = ParseOrNaN(GetLocalAttribute(root, "DesignWidth")?.Value, "DesignWidth");
                             _source.DesignHeight = ParseOrNaN(GetLocalAttribute(root, "DesignHeight")?.Value, "DesignHeight");
-                            _source.Width = GetDimension(root, "Width", "MinWidth", "MaxWidth");
-                            _source.Height = GetDimension(root, "Height", "MinHeight", "MaxHeight");
+
+                            try {
+                               _source.Width = GetDimension(root, "Width", "MinWidth", "MaxWidth");
+                               _source.Height = GetDimension(root, "Height", "MinHeight", "MaxHeight");
+                            }
+                            catch(Exception e) {
+                               _source.Width = ControlDimension.Empty;
+                               _source.Height = ControlDimension.Empty;
+                               Debug.WriteLine("Cannot get view dimensions (width and / or height); " + e.Message);
+                            }

                             Debug.WriteLine("Root Name: " + root.Name.LocalName);

Note: If the above patch looks technically offending :-) please take into consideration I don't have any previous experience with the AvantGarde source code, and I only spent about 5 minutes investigating the problem.

Thank you

Please allow me to take this opportunity to thank you and express my infinite gratitude for developing this indispensable tool.

kuiperzone commented 1 year ago

Let me take a look...

kuiperzone commented 1 year ago

Thank you for the proposed solution. Have implemented in v1.2.2.

iq2luc commented 1 year ago

Thank you for the proposed solution. Have implemented in v1.2.2.

Thank you for the quick resolution and also, again, for this very useful tool (I'm so glad I found it).