Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf .Net 4.5中事件的标记扩展_Wpf_Events_.net 4.5_Markup Extensions_Wpf 4.5 - Fatal编程技术网

Wpf .Net 4.5中事件的标记扩展

Wpf .Net 4.5中事件的标记扩展,wpf,events,.net-4.5,markup-extensions,wpf-4.5,Wpf,Events,.net 4.5,Markup Extensions,Wpf 4.5,WPF不定义用于事件的标记扩展,第三方可以创建可用于事件的标记扩展。现在WPF 4.5支持事件的标记扩展。在.Net 4.5中,有人可以通过一个优雅的示例帮助您实现这一点吗?事件标记扩展允许您对事件使用标记扩展,而在WPF 4.5之前,它们仅可用于属性。例如: <Canvas ClipToBounds="True" Background="White" MouseLeftButtonDown="{local:EventToCommand StartPaintCommand}

WPF不定义用于事件的标记扩展,第三方可以创建可用于事件的标记扩展。现在WPF 4.5支持事件的标记扩展。在.Net 4.5中,有人可以通过一个优雅的示例帮助您实现这一点吗?

事件标记扩展允许您对事件使用标记扩展,而在WPF 4.5之前,它们仅可用于属性。例如:

<Canvas ClipToBounds="True" Background="White"
        MouseLeftButtonDown="{local:EventToCommand StartPaintCommand}"
        MouseMove="{local:EventToCommand AddLineCommand}"
        MouseLeftButtonUp="{local:EventToCommand EndPaintCommand}">
</Canvas>


可以找到完整的示例。

命令

{eb:EventBinding}(查找命令的简单命名模式)

{eb:EventBinding命令=CommandName}

命令参数

$e(事件数)

$this或$this.Property


以下是我编写的一个非常通用的标记扩展的示例,它可以将事件直接绑定到视图模型上的方法:

用法:

<!--  Basic usage  -->
<Button Click="{data:MethodBinding OpenFromFile}" Content="Open" />

<!--  Pass in a binding as a method argument  -->
<Button Click="{data:MethodBinding Save, {Binding CurrentItem}}" Content="Save" />

<!--  Another example of a binding, but this time to a property on another element  -->
<ComboBox x:Name="ExistingItems" ItemsSource="{Binding ExistingItems}" />
<Button Click="{data:MethodBinding Edit, {Binding SelectedItem, ElementName=ExistingItems}}" />

<!--  Pass in a hard-coded method argument, XAML string automatically converted to the proper type  -->
<ToggleButton Checked="{data:MethodBinding SetWebServiceState, True}"
                Content="Web Service"
                Unchecked="{data:MethodBinding SetWebServiceState, False}" />

<!--  Pass in sender, and match method signature automatically -->
<Canvas PreviewMouseDown="{data:MethodBinding SetCurrentElement, {data:EventSender}, ThrowOnMethodMissing=False}">
    <controls:DesignerElementTypeA />
    <controls:DesignerElementTypeB />
    <controls:DesignerElementTypeC />
</Canvas>

    <!--  Pass in EventArgs  -->
<Canvas MouseDown="{data:MethodBinding StartDrawing, {data:EventArgs}}"
        MouseMove="{data:MethodBinding AddDrawingPoint, {data:EventArgs}}"
        MouseUp="{data:MethodBinding EndDrawing, {data:EventArgs}}" />

<!-- Support binding to methods further in a property path -->
<Button Content="SaveDocument" Click="{data:MethodBinding CurrentDocument.DocumentService.Save, {Binding CurrentDocument}}" />

你到底想完成什么?在XAML?Justin中注册事件处理程序-感谢您的快速响应。。我发现了一个新特性,搜索了一些好的示例,但没有得到任何结果samples@VimalCk,我也一直在做这个。只需提到4.5还允许您以反映属性的方式使用泛型,这大大加快了标记扩展的速度@加里,当然,谢谢你的建议。如果微软继续投资WPF。。。我只希望所有这些功能都能在WinRT中使用。。。或者他们提出的任何其他半生不熟的框架。@Adi,谢谢你,伙计,我已经测试了这个应用程序,它的工作非常好。这是WPF中缺少的一个杀手级功能。微软的很多技术对我来说毫无意义。其中最重要的一点是命令和事件之间的任意区别。UI只是一个事件源,但它无法提供一种可靠的方法,将过多的事件返回到视图模型,而无需求助于代码隐藏。UH他们最终的解决方案非常冗长,需要第三个库…嗯。。。
public void OpenFromFile();
public void Save(DocumentModel model);
public void Edit(DocumentModel model);

public void SetWebServiceState(bool state);

public void SetCurrentElement(DesignerElementTypeA element);
public void SetCurrentElement(DesignerElementTypeB element);
public void SetCurrentElement(DesignerElementTypeC element);

public void StartDrawing(MouseEventArgs e);
public void AddDrawingPoint(MouseEventArgs e);
public void EndDrawing(MouseEventArgs e);

public class Document
{
    // Fetches the document service for handling this document
    public DocumentService DocumentService { get; }
}

public class DocumentService
{
    public void Save(Document document);
}