小编Jin*_*ung的帖子

为什么要避免WPF MVVM模式中的代码隐藏?

在文章中,使用Model-View-ViewModel设计模式的WPF应用程序,Josh Smith的作者说:

(1)在设计良好的MVVM体系结构中,大多数视图的代码隐藏应该是空的,或者最多只包含操作该视图中包含的控件和资源的代码.(2)有时还需要在与ViewModel对象交互的View的代码隐藏中编写代码,例如挂钩事件或调用一个本来很难从ViewModel本身调用的方法.

我的问题是,在(1),为什么空代码被认为是一个设计良好的MVVM.(听起来空的代码隐藏总是很好.)

编辑:我的问题是,如下,为什么像的方法AttachedCommandBehavior或者InvokeCommandAction是试图避免代码隐藏编码.

让我解释一下细节.

就(1)而言,我会认为以下情况来自AttachedCommandBehavior.由于Border没有实现ICommandSourcefor MouseRightButtonDown,你不能通常绑定事件和ICommand,但可以使用AttachedCommandBehavior.

<!-- I modified some code from the AttachedCommandBehavior to show more simply -->
<Border>
    <local:CommandBehaviorCollection.Behaviors>
           <local:BehaviorBinding Event="MouseRightButtonDown" 
                  Command="{Binding SomeCommand}" 
                  CommandParameter="A Command on MouseRightButtonDown"/>
    </local:CommandBehaviorCollection.Behaviors>
</Border>
Run Code Online (Sandbox Code Playgroud)

要么

我们可以这样做System.Windows.Interactivity.InvokeCommandAction.

<Border xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseRightButtonDown">
            <i:InvokeCommandAction Command="{Binding SomeCommand}" 
               CommandParameter="A Command on MouseRightButtonDown"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Border>
Run Code Online (Sandbox Code Playgroud)

但,

我们使用以下XAML及其代码隐藏Border_MouseRightButtonDown方法,该方法与上述(2)Josh Simth相关联.

<Border MouseRightButtonDown ="Border_MouseRightButtonDown"/>
Run Code Online (Sandbox Code Playgroud)

我认为使用上面的代码隐藏并不坏,只是因为它们之间的区别只是绑定命令或添加事件处理程序.

你怎么看待这件事?

xaml code-behind mvvm

47
推荐指数
3
解决办法
3万
查看次数

在.net 4.0上执行应用程序时,在.net 2.0下编译

假如说:

  1. 下面的C#源代码是在.NET 2.0(CLR 2.0)下编译的; 和
  2. 以上申请使用app.config下面列出的; 和
  3. 只有.NET 4.0(CLR 4.0)安装在执行应用程序的客户端的环境中,

然后在内部加载哪个版本的.NET来在客户端环境中执行应用程序?

描述

下面的控制台应用程序将只显示其CLR版本v4.0.30319在控制台中,但@Reed Copsey对堆栈的回答(CLR 2.0与4.0性能?)表明在这种情况下加载了.NET 2.0.此外,在MSDN上,它表示何时useLegacyV2RuntimeActivationPolicy设置为false false:

使用.NET Framework 4及更高版本的默认激活策略, 即允许旧版运行时激活技术将CLR版本1.1或2.0加载到该过程中.

尽管app.config有.NET 4.0配置,但听起来像是加载了.NET 2.0 .我误解了什么吗?

资源

C#源代码

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string version = Environment.Version.ToString();
            Console.WriteLine(version);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

app.config

<?xml version="1.0"?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="false">
        <supportedRuntime version="v4.0.30319"/>
    </startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)

.net c# version .net-4.0 .net-2.0

20
推荐指数
1
解决办法
7127
查看次数

如何在WPF WebBrowser中使用"后退"和"前进"导航按钮事件?

WebBrowserWPF中的控件继承自UIElement,但我们无法在UIElement事件中注册事件处理程序.为什么?在WPF WebBrowser鼠标事件没有按预期工作,它得到了回答,但我仍然无法理解.

无论如何,将处理程序连接到WebBrowser可以捕获大多数鼠标事件的文档提供的事件但不能使用"后退"和"前进"导航按钮事件.由于互联网浏览器可以做到这一点,我认为这是可能的.有什么方法可以解决这个问题吗?

更新: 在这个问题中,'Back' & 'Forward' navigation buttons5键鼠标系统中的XButton1和XButton2.

更新2:我用Navid Rahmani的答案解决了这个问题.我认为有人会需要这个答案,所以我附上了主要部分.如果发现任何问题或更合理的解决方案,请告诉我.

    //This code assumes the `WebBrowser` field named _webBrowser is already initiated.
    //For the detail out of this code, please refer to the Navid Rahmani's answer.

    private bool _isMouseOver;
    private HTMLDocumentEvents2_Event _docEvent;    

    public ctor()
    {
        _webBrowser.LoadCompleted += _webBrowser_LoadCompleted;
    }

    private void _webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        if (_docEvent != null)
        {
            _docEvent.onmouseover -= _docEvent_onmouseover;
            _docEvent.onmouseout -= _docEvent_onmouseout;
        } …
Run Code Online (Sandbox Code Playgroud)

c# wpf webbrowser-control

8
推荐指数
1
解决办法
5689
查看次数

标签 统计

c# ×2

.net ×1

.net-2.0 ×1

.net-4.0 ×1

code-behind ×1

mvvm ×1

version ×1

webbrowser-control ×1

wpf ×1

xaml ×1