WPF将.rtf文件从资源加载到RichTextBox [英] WPF Load a .rtf file from Resources to RichTextBox

查看:104
本文介绍了WPF将.rtf文件从资源加载到RichTextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想加载.rtf文件的内容,确切地说,我已将其放入资源(通过Project-> Properties-> Resources-> Add File放入资源中,而我是WPF窗口应用程序)

我想将Agreement.rtf的内容加载到RichTextBox中,我尝试了以下操作

Hi, I am tying to load the content of a .rtf file, which i have put inside Resources (through Project->Properties->Resources->Add File, to be precise, and mine is a WPF window application)

I want to load Agreement.rtf''s content to a RichTextBox, i have tried the following

Dim stream As Stream
stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(My.Resources.ResourceManager.GetObject("Agreement").GetType(), "IOpzioni.Agreement.rtf")
RichTextBox1.SelectAll()

        RichTextBox1.Selection.Load(stream, DataFormats.Rtf)




also

stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(My.Resources.Agreement.GetType(), "IOpzioni.Agreement.rtf")


IOpzioni是我的默认名称空间,我已经仔细检查了
但是似乎没有任何效果,
正确的方法是什么?


Where IOpzioni is my default namespace, i have doublechecked that
But nothing seems to work,
What is the correct Method to do this?

推荐答案

您的代码有点过于复杂,您正在使用多种方法来获取所有资源.同时.如果您不遵守RichTextBox的要求,该解决方案将非常容易.

RTB的LoadFile方法的重载之一采用Stream参数.由于您的资源中已经有文档,因此只需要将该资源放入某种Stream中即可.

在System.Io命名空间中,您将找到一个MemoryStream类.这会将您将数据用作文件而不是磁盘上的数据加载到内存中.因此,您只需要将资源的字节放入A MemoryStream对象中,然后就可以将其传递给RTB的LoadFile方法.
Your code is a bit overly complicated and you''re using pieces of multiple methods to get the resources all at the same time. The solution is pretty easy if you go backwards from the RichTextBox requirements.

One of the overloads of the RTB''s LoadFile method takes a Stream argument. Since you already have the document in your resources, you just need to get that resource into a Stream of some kind.

Looking in the System.Io namespace, you''ll find a MemoryStream class. This will load you use data as a file in memory instead of on a disk. So, you just have to get the bytes of the resource into A MemoryStream object, then you can pass that to the RTB''s LoadFile method.
Using ms As New MemoryStream()
    '' The following Resource Name will probably need to be adjusted.
    Dim buffer as Byte() = Encoding.UTF8.GetBytes(My.Resources.IOpzioni.Agreement)
    ms.Write(buffer, 0, buffer.Length)
    ms.Seek(0, SeekOrigin.Begin)
    RichTextBox1.LoadFile(ms, RichTextBoxStreamType.RichText)
End Using


这篇关于WPF将.rtf文件从资源加载到RichTextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆