0

私の MainWindow には、FlowDocumentScrollViewerそのプロパティDocumentMainViewModelFlowDocumentの aにバインドしています。

このドキュメントは、リモート コンピューター上の外部 xaml ファイル ストアから読み込まれます。現在、このドキュメントを で適切に読み込んXamlReader.Load(xamlfile)で表示することができFlowDocumentScrollViewerます。ここまでは順調ですね。

このドキュメントにハイパーリンクを追加しようとすると、問題が発生します。RequestNavigateイベントを処理するにはx:Class. MainWindowイベントはコード ビハインドでハンドルされるため、当分の間、このクラスは my である必要があります。明らかに、外部ドキュメントを追加すると、解析時に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

<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 ブロックでもプログラムが停止するからです。

よろしくお願いします。

バスティアン。

4

1 に答える 1

1

私は自分の問題に対処する方法を見つけました。それは本当に美しくなく、mvvmの精神を尊重していませんが、まあ、これはうまくいっています。

したがって、実行時に追加することはできないため(推測)、実行時にそれぞれのイベントをx:Class処理するという考えに至りました。したがって、解決策は非常に単純です (そして汚れています)。RequestNavigateHyperlink

コード ビハインド (はい、わかりません) では、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;
     }
 }

誰かがより良い解決策を持っているなら、私はそれを取ります。

于 2014-06-12T11:42:43.823 に答える