【问题标题】:Telerik PdfViewer from MVVM View Not Displaying from Byte Stream来自 MVVM 视图的 Telerik PdfViewer 未从字节流中显示
【发布时间】:2018-07-04 17:28:42
【问题描述】:

我正在尝试对 pdfviewer 使用 MVVM 绑定,但文档总是显示为空白。我正在查询一个包含 PDF 文件字节的 DB 字段,并试图让它在查看器中显示(我已经确认该文件没有格式错误,如果我将其保存为 PDF 则显示正常到我的桌面)。

这是我的 MVVM 属性:

private RadFixedDocument _currentDocument;
public RadFixedDocument CurrentDocument
{
    get => _currentDocument; set => this.RaiseAndSetIfChanged(ref _currentDocument, value);
}

这是我用来填充 CurrentDocument 属性的方法:

var sqlStr = @"select top 1 * from FileManager order by ID desc;";
var file = await con.QuerySingleAsync<FMFile>(sqlStr);
var fileBytes = file.FileContent.ToArray();  // this is type byte[] - I confirmed that it has the correct contents
var ms = new MemoryStream(fileBytes); // ms gets filled properly
FormatProviderSettings settings = new FormatProviderSettings(ReadingMode.AllAtOnce);
PdfFormatProvider provider = new PdfFormatProvider(ms, settings);
CurrentDocument = provider.Import();

这是我的 XAML 的外观:

<telerik:RadPdfViewerToolBar RadPdfViewer="{Binding ElementName=pdfViewer, Mode=OneTime}" />
<telerik:RadPdfViewer x:Name="pdfViewer"
                              Grid.Row="1"
                              DocumentSource="{Binding CurrentDocument, Mode=TwoWay}" />

但是,查看器中没有显示任何内容。我确认我的 VM 已正确连接,因为其他属性显示正常。任何建议将不胜感激。

【问题讨论】:

  • RaiseAndSetIfChanged 是否触发 INotifyPropertyChange?
  • 是的,它是来自名为 Reactiveui 的库中的一种方法。

标签: wpf pdf mvvm telerik


【解决方案1】:

看来您绑定到了错误的对象类型。

绑定到类型 PdfDocumentSource,而不是 RadFixedDocument。

private PdfDocumentSource _PDFDocument;
public PdfDocumentSource PDFDocument
    {
        get { return _PDFDocument; }
        set
        {
            _PDFDocument = value;
            OnPropertyChanged("PDFDocument");
        }
    }

private void LoadPDFDocument(string pdfFilePath)
{
    MemoryStream stream = new MemoryStream();

    using (Stream input = File.OpenRead(pdfFilePath))
    {
        input.CopyTo(stream);
    }

    FormatProviderSettings settings = new FormatProviderSettings(ReadingMode.OnDemand);
    PDFDocument = new PdfDocumentSource(stream, settings);
}

【讨论】:

  • 我也试过这个(尽管在你的例子中你是从文件中读取的)。不幸的是,我得到了相同的结果(一个空白窗口)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-02
  • 2016-06-23
  • 1970-01-01
  • 2020-07-13
  • 1970-01-01
  • 2015-05-02
  • 2021-04-04
相关资源
最近更新 更多