2014-06-12 7 views
0

을 나는 FlowDocumentScrollViewerMainViewModelFlowDocument에 해당 속성 Document 바인딩이있다.FlowDocument 및 XamlReader X : 클래스 내 MainWindow를에서

이 문서는 원격 컴퓨터의 외부 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 그런데

<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 블록에서도 내 프로그램을 중지하기 때문에이 예외를 파싱합니다 (잘못된 외부 파일의 경우). 사전에

감사를,

Bastien.

답변

1

내 문제를 해결할 방법을 찾았지만 정말 아름답 지 않아서 mvvm의 정신을 존중하지는 않지만 잘하고 있습니다. 이 런타임 (내 생각)에서 x:Class을 추가 할 수 없습니다로

그래서, 나는 런타임 각 Hyperlink RequestNavigate 이벤트를 처리하는 생각을했다. 따라서 솔루션은 매우 단순합니다.

코드 숨김에서 (예, 나도 알아, 못생긴) 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; 
    } 
} 

경우에, 나는 그것을 가지고 : 여기

몇 가지 코드입니다.