【发布时间】: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