xaml 转换 html,Convert XAML to HTML

Thank you for your reply, but the image still can't be located. Maybe my description is a little bit bad. (or my english :))

Better Description: :)

1. Create a wpf application

2. Add a rich textbox to the application and name it "RichTextbox"

3. Create an "Convert To XAML String" button and insert this source code

Dim XamlString As String

Dim ms As New IO.MemoryStream

Dim xaml As Windows.Markup.XamlDesignerSerializationManager = New Windows.Markup.XamlDesignerSerializationManager(New Xml.XmlTextWriter(ms, System.Text.ASCIIEncoding.UTF8))

xaml.XamlWriterMode = Windows.Markup.XamlWriterMode.Expression

Windows.Markup.XamlWriter.Save(RichTextbox.Document, xaml)

ms.Seek(0, IO.SeekOrigin.Begin)

Dim stReader As IO.StreamReader = New IO.StreamReader(ms)

XamlString = stReader.ReadToEnd

4. Create an "Add Image" button and insert the following source

Dim ofd As New System.Windows.Forms.OpenFileDialog

ofd.Filter = "Image files (*.png;*.gif;*.jpg;*.jpeg)|*.png;*.gif;*.jpg;*.jpeg"

ofd.Multiselect = False

If ofd.ShowDialog = DialogResult.OK Then

Dim img As New Windows.Controls.Image

Dim bitmapImg As New Windows.Media.Imaging.BitmapImage

bitmapImg.BeginInit()

bitmapImg.UriSource = New Uri(ofd.FileName)

bitmapImg.EndInit()

img.Width = bitmapImg.Width

img.Height = bitmapImg.Height

img.Source = bitmapImg

Dim inline As New Windows.Documents.InlineUIContainer

inline.Child = img

If RichTextbox.Selection.Start.Paragraph Is Nothing Then

RichTextbox.Selection.Start.InsertParagraphBreak()

End If

RichTextbox.Selection.Start.Paragraph.Inlines.Add(inline)

End If

5. Run the application and add an image to the richtextbox

6. Drag and Drop the image arround

7. Press the "Convert To XAML String" button

8. Now the String XamlString will contain the following xaml

9. You will see that the image you added is located at ./Image1.bmp

10. Now my problem is to get the stream of the image :)

You can try to run the example you posted and you will see that i won't work.

Also I have to say that it isn't a possibility to create a collection of all added images because if there are many images in the richtextbox and they are moved arround i can't find out which image is which.

Thank you for your help!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在XAML中使用了逗号或空格分隔字符串来设置用户控件的数组属性值,但是编译器提示无法转换类型,那么可能是因为你的用户控件的数组属性没有实现`TypeConverter`接口。 在WPF中,`TypeConverter`接口是用于将一种类型的值转换为另一种类型的值的机制。如果我们在自定义控件中使用了自定义的数据类型,那么我们需要为这些数据类型实现`TypeConverter`接口,以便WPF能够正确地将这些数据类型转换为其他类型。 下面是一个示例: 假设我们有一个名为`MyControl`的控件,在该控件中有一个名为`MyArray`的自定义数据类型的数组属性。我们需要为该自定义数据类型实现`TypeConverter`接口,以便WPF能够正确地将字符串转换为该数据类型的数组。 ``` public class MyDataType { public string Value1 { get; set; } public int Value2 { get; set; } } public class MyDataTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string[] strArray = value.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); List<MyDataType> list = new List<MyDataType>(); foreach (string str in strArray) { string[] values = str.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); if (values.Length == 2) { list.Add(new MyDataType { Value1 = values[0], Value2 = int.Parse(values[1]) }); } } return list.ToArray(); } } ``` 在上面的代码中,我们定义了一个名为`MyDataType`的自定义数据类型,其中包含两个属性`Value1`和`Value2`。然后我们定义了一个名为`MyDataTypeConverter`的类型转换器,实现了`TypeConverter`接口中的`CanConvertFrom`和`ConvertFrom`方法。在`ConvertFrom`方法中,我们使用逗号和竖线(`|`)分隔符来将输入的字符串转换为`MyDataType`类型的对象,并将这些对象添加到一个`List`中,最后将该`List`转换为一个`MyDataType[]`类型的数组。 接下来,在我们的`MyControl`控件中,我们需要将`MyArray`属性的类型定义为`MyDataType[]`类型,并为该属性应用`TypeConverter`特性,以便WPF能够正确地将字符串转换为该数据类型的数组。 ``` [TypeConverter(typeof(MyDataTypeConverter))] public MyDataType[] MyArray { get { return (MyDataType[])GetValue(MyArrayProperty); } set { SetValue(MyArrayProperty, value); } } ``` 在上面的代码中,我们使用`TypeConverter`特性来指定`MyDataTypeConverter`类型转换器,这样WPF在将字符串转换为`MyDataType[]`类型的数组时就会使用该类型转换器。 最后,在XAML中我们就可以使用逗号和竖线分隔符来为`MyArray`属性赋值,如下所示: ``` <local:MyControl MyArray="Value 1|1,Value 2|2,Value 3|3" /> ``` 在上面的代码中,我们使用逗号和竖线分隔符来将三个`MyDataType`类型的对象转换为一个`MyDataType[]`类型的数组,并将该数组赋值给`MyArray`属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值