Creating markup extensions using a converter

I am trying to create a markup extension that will take an HTML string, convert it to a FlowDocument, and return a FlowDocument. I am new to creating markup extensions, and I hope this will be obvious to people with a lot of experience. Here is my code:

[MarkupExtensionReturnType(typeof(FlowDocument))] public class HtmlToXamlExtension : MarkupExtension { public HtmlToXamlExtension(String source) { this.Source = source; } [ConstructorArgument("source")] public String Source { get; set; } public Type LocalizationResourceType { get; set; } public override object ProvideValue(IServiceProvider serviceProvider) { if (this.Source == null) { throw new InvalidOperationException("Source must be set."); } FlowDocument flowDocument = new FlowDocument(); flowDocument.PagePadding = new Thickness(0, 0, 0, 0); string xaml = HtmlToXamlConverter.ConvertHtmlToXaml(Source.ToString(), false); using (MemoryStream stream = new MemoryStream((new ASCIIEncoding()).GetBytes(xaml))) { TextRange text = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd); text.Load(stream, DataFormats.Xaml); } return flowDocument; } } 

Update: here is XAML.

 <RadioButton.ToolTip> <FlowDocumentScrollViewer Document="{ext:HtmlToXaml Source={x:Static res:ExtrudeXaml.RadioButtonCreateBody_TooltipContent}}" ScrollViewer.VerticalScrollBarVisibility="Hidden" /> </RadioButton.ToolTip> 

And my list of VS errors:

  • Error 3 The unknown "Source" property for the type "MS.Internal.Markup.MarkupExtensionParser + UnknownMarkupExtension" encountered when parsing the Markup Extension. Line 89 Position 49.
  • Error 1 Type "HtmlToXamlExtension" does not contain a constructor with the specified number of arguments.
  • Error 2 No constructor for type 'HtmlToXamlExtension' has 0 parameters.
+4
source share
3 answers

You implemented MarkupExtension without a default constructor: Thus, you have 2 options:

  • Delete your specific constructor (in any case, you installed Source directly)
  • Modify the HtmlToXamlExtension call if you remove the Source= part, then Wpf will try to find the constructor that matches all the unnamed fields immediately after the ext:HtmlToXaml part:

     <RadioButton.ToolTip> <FlowDocumentScrollViewer Document="{ext:HtmlToXaml {x:Static res:ExtrudeXaml.RadioButtonCreateBody_TooltipContent}}" ScrollViewer.VerticalScrollBarVisibility="Hidden" /> </RadioButton.ToolTip> 

    UPD: Although it works, but MSDN says you should have a default constructor

Hope this helps.

+3
source

You must create a default constructor to expand the markup, and everything will be fine.

0
source

This helped me install .NET 4.7 (Developer Pack), I saw this error in .NET 4.6, but after the update it was gone.

0
source

Source: https://habr.com/ru/post/1487272/


All Articles