1

我只需要在主代码中获取 FlowDocument ...

我喜欢这样,但没有成功

this.flowDoc = engine.CreateFlowDocument(contentItems); // Can't compile
 public async Task<FlowDocument> CreateFlowDocument(ContentItems contentItems)
 {   return await Task.Run(() =>
     {
         try
         {
             var fd = new FlowDocument();
             foreach (var item in contentItems.Items)
             {
                 if (item.ContentType == ContentTypes.Header)
                 {
                     var paraHeader = new Paragraph();  
                     paraHeader.FontSize = 14;
                     paraHeader.Foreground = Brushes.Black;
                     paraHeader.FontWeight = FontWeights.Bold;
                     paraHeader.Inlines.Add(new Run(item.Data));
                     fd.Blocks.Add(paraHeader);
                 }
                 if (item.ContentType == ContentTypes.Paragraph)
                 {
                      var paragraph = new Paragraph();
                      paragraph.FontSize = 12;
                      paragraph.Foreground = Brushes.Black;
                      paragraph.FontWeight = FontWeights.Normal;
                      paragraph.Inlines.Add(new Run(item.Data));
                      fd.Blocks.Add(paragraph);
                 }
             }                     
             return fd;
          }
          catch (Exception)     {                        }
          return new FlowDocument();
      }
                                 );
}
4

1 回答 1

5

您需要等待您返回的任务CreateFlowDocument

 this.flowDoc = await engine.CreateFlowDocument(contentItems);

当然,这意味着代码也必须在异步函数中。

如果您不希望它在异步方法中,您可以使用该Result属性,但请务必小心操作 - 这是一个阻塞调用,您很容易陷入僵局。

或者您可以使用Task.ContinueWith.

于 2013-04-12T13:33:01.483 回答