在UWP中使用RichEditBox只读时图片显示问题(最后有关于RichEditBox颜色问题的解决方案)

在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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值