wpf icon indicating copy to clipboard operation
wpf copied to clipboard

In .NET 9 Preview 6, serialization of style using XamlWriter and XamlReader throws exception

Open KarkuvelRajan opened this issue 1 year ago • 3 comments

Description

In .NET 9 Preview 6, serialization and deserialization of style using XamlWriter and XamlReader throws exception. But in .NET 8 and other framework versions, it works fine. Please find the code below.

In .NET 9 Preview 6, most of the setter tags do not have <Setter.Value> tags in the output file which results in the exception.

public string filePath = "style.xml";

StringWriter stringWriter = new StringWriter();

// for saving
Style style = new Style(typeof(System.Windows.Shapes.Path));
style.Setters.Add(new Setter(System.Windows.Shapes.Path.StretchProperty, Stretch.Fill));
style.Setters.Add(new Setter(System.Windows.Shapes.Path.FillProperty, Brushes.Black));            

System.Windows.Markup.XamlWriter.Save(style, stringWriter);

File.WriteAllText(filePath, stringWriter.ToString());

// for loading

string xamlContent = File.ReadAllText(filePath);

// Parse the XAML content back into a Style object
using (StringReader stringReader = new StringReader(xamlContent))
{
    using (XmlReader xmlReader = XmlReader.Create(stringReader))
    {
        Style loadedStyle = (Style)XamlReader.Load(xmlReader);                    
    }
}

Reproduction Steps

Use the above codes to replicate the issue.

Expected behavior

Like other Net Frameworks and Core versions, the XAMLWriter should create the <Setter.Value> tag for the Fill property.

Actual behavior

the XAMLWriter has not created the <Setter.Value> tag for the Fill property.

Regression?

No response

Known Workarounds

No response

Configuration

Net 9 Preview 6

Other information

No response

KarkuvelRajan avatar Jul 11 '24 10:07 KarkuvelRajan

The Setter.Value was made optional so the writer is theoretically fine, but the reader should be able to deal with that.

miloush avatar Jul 11 '24 14:07 miloush

@KarkuvelRajan - This issue has been fixed with PR - https://github.com/dotnet/wpf/pull/9229. Please check.

anjaligupta-dev avatar Jul 21 '24 06:07 anjaligupta-dev

Hi @anjali-wpf,

Can you please update on which version of the Net9 this fix will be available?

KarkuvelRajan avatar Aug 06 '24 04:08 KarkuvelRajan