Open frankhaugen opened 3 months ago
I think it is the generic type issues...
The exception throw in MarkupWriter.VerifyTypeIsSerializable
:
It means that this exception is as wpf's design. Maybe we should update the document.
... It means that this exception is as wpf's design. Maybe we should update the document.
Yes, as if this is "per design" then I would like something to tell me, with XML-docs, docs about the functionality, analyzer and maybe a more clear exception message
Yeah, I agree with you. I think we should update the document.
I think you have other issues. How do you expect the DisplayFunc and SelectionChangedAction to be serialized? The private FuncValueConverter?
I think you have other issues. How do you expect the DisplayFunc and SelectionChangedAction to be serialized? The private FuncValueConverter?
Why would I want to serialize it? They are behavioral
Why would I want to serialize it? They are behavioral
How would the serializer know? They are public properties and have values, so they are subject to serialization. You are giving the "final" object to the serializer, so for example the ComboBox has the ItemTemplate set, which should be written out during serialization. Your expected output does not have it.
Why would I want to serialize it? They are behavioral
How would the serializer know? They are public properties and have values, so they are subject to serialization. You are giving the "final" object to the serializer, so for example the ComboBox has the ItemTemplate set, which should be written out during serialization. Your expected output does not have it.
Well, it's not causing an issue as far as I see, anyway it was a simple, and discoverable way to have a "template". I must admit I am not a WPF dev, so I'm skipping the XAML-parts and making everything how I would build components for a backend microservice 😆
Right, so there is two XAML schemas, 2006 and 2009. The latter supports generics. See https://learn.microsoft.com/en-us/dotnet/desktop/xaml-services/generics. The compiler does not support 2009, but that shouldn't be an issue for your use case.
There is also two XAML writers/readers, one in System.Windows.Markup namespace and one in System.Xaml namespace. The reader/loader situation is a bit easier because the Markup one uses the Xaml one internally and supports generics and most of the other 2009 features.
The writer situation is slightly less transparent. It is true that the System.Windows.Markup.XamlWriter does not support generics and I agree it would be nice if it did. The System.Xaml one does support generics. You don't really want to use it to generate XAML from WPF for users, because it serializes way more than is needed, but that might not be an issue for you either. However, I cannot simply suggest you use that one instead, because as I noted, you have bunch of other issues, such as non-public types and delegates that affect your object. I should have included the intro above, sorry. What I am trying to say is even if the XamlWriter supported generics, you won't be able to serialize your object, so maybe you might need to look for a different solution.
Btw for inspecting WPF apps you can use https://github.com/snoopwpf/snoopwpf.
Related: #58
Description
I have made a WPF dropdown component that is generic, so I can make a Combobox a little less tedious to work with:
Reproduction Steps
I have a basic test using Xunit with XUnit WpfFact nuget, where I would check for elements in the string:
Expected behavior
XAMLWriter.Save(...) to return something like:
Actual behavior
Throws exception:
Regression?
No response
Known Workarounds
Writing one's own "serializer", or rewriting to not use a generic
Impact
Edge case maybe, but I often include a secret key-combo in my WPF apps that will display a "raw dump" of the XAML and whatever else is loaded in the current window
Configuration
Windows 11 .net 8
Other information
Documentation don't state that this is an unreasonable expectation, (accepting generic UiElements): https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/serialization-limitations-of-xamlwriter-save?view=netframeworkdesktop-4.8&viewFallbackFrom=netdesktop-8.0