在UWP中使用RichEditBox只读时图片显示问题(最后有关于RichEditBox颜色问题的解决方案)
来源:互联网 发布:win10升级软件 编辑:程序博客网 时间:2024/05/18 03:13
在UWP中使用RichEditBox只读时图片显示问题(最后有关于RichEditBox颜色问题的解决方案)
今天学到了uwp中还有一个叫richeditbox的控件,感觉rtf还是挺适合做一个阅读器之类的,就简单地写了这么一些代码:
xaml中的代码如下:
<Page x:Class="RichTest.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:RichTest" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <RichEditBox Name="display" IsReadOnly="True"></RichEditBox> </Grid></Page>
cs代码如下:(为了说明省略了一些东西)
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.InteropServices.WindowsRuntime;using System.Threading.Tasks;using Windows.Foundation;using Windows.Foundation.Collections;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Controls.Primitives;using Windows.UI.Xaml.Data;using Windows.UI.Xaml.Input;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;//“空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 上有介绍namespace RichTest{ /// <summary> /// 可用于自身或导航至 Frame 内部的空白页。 /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); Open(); } public async Task<string> Open() { Windows.Storage.Pickers.FileOpenPicker open = new Windows.Storage.Pickers.FileOpenPicker(); open.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; open.FileTypeFilter.Add(".rtf"); Windows.Storage.StorageFile file = await open.PickSingleFileAsync(); //load: if (file != null) { try { Windows.Storage.Streams.IRandomAccessStream randAccStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); //ReadContent.DataContext = randAccStream; display.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf,randAccStream); return file.Path; } catch (Exception) { ContentDialog errorDialog = new ContentDialog() { Title = "File open error", Content = "Sorry, I couldn't open the file.", PrimaryButtonText = "Ok" }; await errorDialog.ShowAsync(); } } //set(ref display, await FileIO.ReadTextAsync(file)); return ""; } }}
运行时发现,原本rtf中的图片读入控件中怎么也显示不出来,而如果在xaml中将richeditbox中的IsReadOnly属性设为false后则图片会正常显示。
这可是苦恼我好一阵子,因为本人想做的是一个阅读器,不想用户修改rtf文件。
查询了msdn上的api依旧找不到问题的解决方案。
本人想到的第一种方法是将richeditbox 的IsEnabled设为false,但是感觉这方法不是很靠谱,背离了原则,治标不治本。
测试了好多种方法后,意外之下发现了一个很不错的解决方案:
那就是
1.在xaml中richEditBox的IsReadOnly属性设置为false
2.然后在代码中加载入rtf文件的数据后,再对richEditBox的ReadOnly属性进行设置,在上面的代码中,也就是在
display.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf,randAccStream);
后面加上:
display.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf,randAccStream);display.IsReadOnly = true;
运行发现图片终于显示出来了。
这个问题我估计大部分人都不会遇到,在这里写的是我的第一篇博客,希望能够让读者少走点弯路。
哦,对了,RichEditBox还有一个字体颜色丢失的问题存在,网上的解决方案如下:
http://blog.csdn.net/igweyou/article/details/50585760
关于这个上面的网站,我尝试后发现需要和
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="RequestedTheme" Storyboard.TargetName="ContentElement"> <DiscreteObjectKeyFrame KeyTime="0" Value="Light"/> </ObjectAnimationUsingKeyFrames>
一起注释后才可以
Ps:排版有些乱,请见谅
1 0
- 在UWP中使用RichEditBox只读时图片显示问题(最后有关于RichEditBox颜色问题的解决方案)
- 【Win 10应用开发】使用RichEditBox控件应注意的问题
- RichEditBox 使用自定义菜单
- RichEditBox控件装载并显示RTF文件内容
- CSS-有关于图片的问题
- 在Android学习中有关于广播注册的问题
- 关于RichEditBox控件
- jsp中有关于超链接的问题
- oracle中有关于字符串的问题。
- 有关于数据中选择的问题
- 【声明】有关于博文中图片不显示问题的声明
- 有关于面试常遇到的问题回答,以及解决方案。
- 有关Applet无法在IE中显示问题的解答!
- 在Ubuntu中使用Python的matplotlib库时图片不能显示问题的解决方法
- 有关于oracle的问题!
- 有关于预处理的问题
- 有关于在Java 类的静态初始化块中创建一个自身实例的问题。
- 有关于mysql 在linux下去分表名大小写的问题
- 2010 Asia Fuzhou Regional Contest 之 A hard Aoshu Problem
- 第四章: 数组
- cvThreshold函数使用与注意事项
- Android 智能指针(1)
- JMS的规范,以及ActiveMQ中p2p和publish/subscriber2中模式
- 在UWP中使用RichEditBox只读时图片显示问题(最后有关于RichEditBox颜色问题的解决方案)
- Spring事务配置的五种方式
- 4456: [Zjoi2016]旅行者 分治+最短路
- oc中block的理解
- 方法:Mac终端控制文件显隐性
- H264--1--编码原理以及I帧B帧P帧、pts&dts
- linux 个性化
- 第五章:函数
- compass 初学笔记一