【问题标题】:PRISM Command to include sender, OnNavigated command to include WebBrowser senderPRISM 命令包括发件人,OnNavigated 命令包括 WebBrowser 发件人
【发布时间】:2020-05-28 17:36:01
【问题描述】:

我想在我的 ViewModel 的 DelegateCommand 中包含“发送者”对象。

<WebBrowser>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Navigated">
            <prism:InvokeCommandAction  
                        Command="{Binding NavigatedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</WebBrowser>


private DelegateCommand<NavigationEventArgs> _onNavigated;
public DelegateCommand<NavigationEventArgs> NavigatedCommand => _onNavigated ?? (_onNavigated = new DelegateCommand<NavigationEventArgs>(OnNavigated, (e) => true));

private void OnNavigated(NavigationEventArgs e)
{
//TODO add code to access the WebBrowser.Document property
}

这不起作用:

private DelegateCommand<WebBrowser, NavigationEventArgs> _onNavigated;

什么是委托命令?

【问题讨论】:

  • 您确定要命令吗?如果您需要控件,您可以使用 ui-stuff,因此只需将其放在代码隐藏中(事件获取发送者)。
  • @Haukinger PRISM MVVM。不是旧的代码隐藏。
  • 你的命令是做什么的?它需要sender 做什么?代码隐藏非常适合做与视图相关的事情。
  • 访问 WebBrowser 控件对象会暴露 Document 属性。
  • 是什么阻止您将Document 放入CommandParameter?

标签: c# wpf command prism


【解决方案1】:

您想使用i:InvokeCommandAction 而不是prism:InvokeCommandAction。

我不知道,为什么棱镜变体不适用于CommandParameter,但交互性却可以...

查看:

<WebBrowser>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Navigated">
            <i:InvokeCommandAction Command="{Binding NavigatedCommand}"
                                   CommandParameter="{Binding Path=Document, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WebBrowser}}}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</WebBrowser>

查看模型:

public SomeWindowViewModel()
{
    NavigatedCommand = new DelegateCommand<object>( x => { /* x is the browser's Document here */ } );
}

public DelegateCommand<object> NavigatedCommand { get; }

【讨论】:

    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 2022-07-19
    • 2017-07-18
    • 2012-09-03
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    相关资源
    最近更新 更多