【问题标题】:FlowDocument and XamlReader x:ClassFlowDocument 和 XamlReader x:Class
【发布时间】:2014-06-12 08:49:25
【问题描述】:

在我的 MainWindow 中,我有一个 FlowDocumentScrollViewer 将其属性 Document 绑定到我的 MainViewModel 中的 FlowDocument

此文档是从远程计算机上的外部 xaml 文件存储加载的。目前,我可以通过XamlReader.Load(xamlfile) 正确加载此文档并将其显示在FlowDocumentScrollViewer 中。到目前为止一切顺利。

当我尝试在此文档中添加超链接时出现问题。因为要处理RequestNavigate 事件,我需要x:Class。暂时这个类需要是我的MainWindow,因为事件是在代码隐藏中处理的。显然,当我在外部文档中添加x:Class="Ugrader.MainWindow" 时,我会在解析时得到一个可爱的'System.Windows.Markup.XamlParseException'

那么有没有办法解决这个问题?

这是我的一段代码

MainWindow.xaml

<Window x:Class="Ugrader.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Geco3-Upgrading version" 
        WindowStyle="none" ResizeMode="NoResize" ShowInTaskbar="False" WindowStartupLocation="CenterScreen"
        Height="400" Width="700"
        DataContext="{Binding Main,Source={StaticResource Locator}}">


        <FlowDocumentScrollViewer Grid.Column="1" Background="{x:Null}" VerticalScrollBarVisibility="Hidden"
                                      Document="{Binding WhatsNewDoc}"/>

</Window>

MainViewModel.cs

namespace Ugrader.ViewModel
{
    public class MainViewModel : ViewModelBase
    {        
        #region Constructor
        public MainViewModel()
        {               
            try
            {
                FileStream xamlFile = new FileStream(updateLocation + "whatsnew.xaml", FileMode.Open, FileAccess.Read);
                FlowDocument current = System.Windows.Markup.XamlReader.Load(xamlFile) as FlowDocument;
                WhatsNewDoc = current;
            }
            catch (Exception)
            {  

            }
        }
        #endregion 

        #region Properties
        private FlowDocument _watsNewDoc = new FlowDocument();
        public FlowDocument WhatsNewDoc
        {
            get
            {
                return _watsNewDoc;
            }
            set
            {
                if(_watsNewDoc != value)
                {
                    _watsNewDoc = value;
                    RaisePropertyChanged("WhatsNewDoc");
                }
            }
        }
        #endregion
    }
}

外部流文档

<FlowDocument x:Class="Ugrader.MainWindow" 
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              ColumnWidth="400" FontSize="12" FontFamily="Century Gothic" Foreground="LightGray">

  <Paragraph>
    <Italic>
      For additionnal information, please watch this
      <Hyperlink TextDecorations="{x:Null}" RequestNavigate="Hyperlink_Clicked" NavigateUri="path_to_the_file" >video</Hyperlink>
    </Italic>
  </Paragraph>

</FlowDocument>

顺便说一句,有没有办法处理这个解析异常(在外部文件错误的情况下),因为即使在这个 try/catch 块中,这也会停止我的程序。

提前谢谢你,

巴斯蒂安。

【问题讨论】:

    标签: c# wpf xaml flowdocument xamlreader


    【解决方案1】:

    我找到了解决我的问题的方法,它不是很漂亮,不尊重 mvvm 的精神,但是,这行得通。

    所以,由于不可能在运行时添加x:Class(我猜),我想到了在运行时处理每个HyperlinkRequestNavigate 事件在运行时。所以解决方案非常简单(而且很脏)。

    在代码隐藏中(是的,我知道,这很丑),在 MainWindow 加载事件中,我在我的文档中找到所有超链接,并处理每个 RequestNavigate 事件。就这么简单(又脏)。

    这是一些代码:

     private void Window_Loaded(object sender, RoutedEventArgs e)
     {
        var hyperlinks = GetVisuals(this).OfType<Hyperlink>();
        foreach (var link in hyperlinks)
        link.RequestNavigate += link_RequestNavigate;
     }
    
     public static IEnumerable<DependencyObject> GetVisuals(DependencyObject root)
     {
         foreach (var child in LogicalTreeHelper.GetChildren(root).OfType<DependencyObject>())
         {
             yield return child;
             foreach (var descendants in GetVisuals(child))
                 yield return descendants;
         }
     }
    

    如果有人有更好的解决方案,我会接受。

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      相关资源
      最近更新 更多