在我的 MainWindow 中,我将FlowDocumentScrollViewer
其属性绑定Document
到FlowDocument
我的MainViewModel中。
该文档是从远程计算机上的外部 xaml 文件存储加载的。目前,我可以通过正确加载此文档XamlReader.Load(xamlfile)
并将其显示在FlowDocumentScrollViewer
. 到目前为止,一切都很好。
当我尝试在此文档中添加超链接时出现问题。因为要处理RequestNavigate
事件,我需要一个x:Class
. 暂时这个类需要是我MainWindow
的,因为事件是在代码隐藏中处理的。显然,当我添加我的外部文档时,我在解析的那一刻x:Class="Ugrader.MainWindow"
得到了一个可爱的结果。'System.Windows.Markup.XamlParseException'
那么有没有办法解决这个问题?
这是我的一段代码
主窗口.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>
主视图模型.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 块中,这也会停止我的程序。
提前谢谢你,
巴斯蒂安。