刘sir

春不是叫出来的,是真刀实枪干出来的!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

次线程不能直接修改主线程UI的界面,需要使用以下方法

this.Dispatcher.Invoke(DispatcherPriority.Normal,
new Action(() =>
{
  //调用主线程UI的的代码                           
}));

如:

void LoadFile()
        {
            try
            {
                if (string.IsNullOrEmpty(filename))
                {
                    //加载建设中
                    return;
                }
                if (!File.Exists(filename))
                {
                    //加载建设中
                    return;
                }
                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
                    {
                        string ext = System.IO.Path.GetExtension(filename);
                        System.Windows.Documents.FlowDocument flowDocument = new System.Windows.Documents.FlowDocument();
                        flowDocument.IsOptimalParagraphEnabled = true;
                        flowDocument.FlowDirection = System.Windows.FlowDirection.LeftToRight;

                        flowDocument.IsHyphenationEnabled = true;

                        switch (ext)
                        {
                            case ".rtf":

                                TextRange range = new TextRange(
                                                    flowDocument.ContentStart,
                                                    flowDocument.ContentEnd);
                                range.Load(fs, DataFormats.Rtf);
                                break;
                            default:
                                break;
                        }
                        flowDocument.ColumnWidth = 540;
                        FlowDocPageView1.Document = flowDocument;
                    }

                }), DispatcherPriority.Normal);

            }
            catch { return; }
        }

 

posted on 2013-07-21 12:59  刘sir~  阅读(776)  评论(0编辑  收藏  举报