我想在 WebView 中绑定事件,但出现异常:
Windows.UI.Xaml.Markup.XamlParseException
VK.exe 中出现“ ”类型的异常,但未在用户代码中处理WinRT 信息: 无法将“ ”类型的实例添加到“ ”
Win8nl.Behaviors.EventToCommandBehavior
类型的集合中System.Collections.ObjectModel.ObservableCollection<WinRtBehaviors.Behavior>
您可能知道,Windows SDK没有 System.Interaction.dll。我试图在网络上(为 Windows RT 编译)或 Blend 文件夹中找到它,但找不到。
比我发现Win8nl使用行为允许我在视图模型中连接控件和 RelayCommands 的事件。
他们帮助我绑定加载的事件以供查看:这是代码:
...
xmlns:WinRtBehaviors="using:WinRtBehaviors"
xmlns:Win8nl_Behavior="using:Win8nl.Behaviors"
DataContext="{Binding Login, Source={StaticResource Locator}}">
<WebView x:Name="webView"
Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Left"
VerticalAlignment="Top"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
Height="420" Width="530">
<WinRtBehaviors:Interaction.Behaviors>
<Win8nl_Behavior:EventToCommandBehavior Event="LoadCompleted"
Command="WebViewLoadedCommand" />
</WinRtBehaviors:Interaction.Behaviors>
</WebView>
视图模型:
public LoginViewModel(IDataService dataService)
{
WebViewLoadedCommand = new RelayCommand(() => {
var msg = new MessageDialog("Web View Loaded");
msg.ShowAsync();
});
}
public RelayCommand WebViewLoadedCommand { get; private set; }
我也想获得 NavigatedEventsArgs 参数,就像我为 WP7 和 System.Interaction 所做的那样:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Navigated">
<mvvm:EventToCommand Command="{Binding NavigatedCommand, Mode=OneWay}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
谢谢!