在文章中,使用Model-View-ViewModel设计模式的WPF应用程序,Josh Smith的作者说:
(1)在设计良好的MVVM体系结构中,大多数视图的代码隐藏应该是空的,或者最多只包含操作该视图中包含的控件和资源的代码.(2)有时还需要在与ViewModel对象交互的View的代码隐藏中编写代码,例如挂钩事件或调用一个本来很难从ViewModel本身调用的方法.
我的问题是,在(1),为什么空代码被认为是一个设计良好的MVVM.(听起来空的代码隐藏总是很好.)
编辑:我的问题是,如下,为什么像的方法AttachedCommandBehavior
或者InvokeCommandAction
是试图避免代码隐藏编码.
让我解释一下细节.
就(1)而言,我会认为以下情况来自AttachedCommandBehavior.由于Border没有实现ICommandSource
for 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)
我认为使用上面的代码隐藏并不坏,只是因为它们之间的区别只是绑定命令或添加事件处理程序.
你怎么看待这件事?
假如说:
app.config
下面列出的; 和然后在内部加载哪个版本的.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) WebBrowser
WPF中的控件继承自UIElement
,但我们无法在UIElement
事件中注册事件处理程序.为什么?在WPF WebBrowser鼠标事件没有按预期工作,它得到了回答,但我仍然无法理解.
无论如何,将处理程序连接到WebBrowser
可以捕获大多数鼠标事件的文档提供的事件但不能使用"后退"和"前进"导航按钮事件.由于互联网浏览器可以做到这一点,我认为这是可能的.有什么方法可以解决这个问题吗?
更新:
在这个问题中,'Back' & 'Forward' navigation buttons
5键鼠标系统中的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)