Binding the Loaded event?

后端 未结 5 1146
面向向阳花
面向向阳花 2020-12-24 08:16

I am trying to display a login window once my MainWindow loads while sticking to the MVVM pattern. So I am trying to Bind my main windows Loaded event to an event in my view

5条回答
  •  自闭症患者
    2020-12-24 08:50

    Update:

    I made a post about a new more flexible version of the method binding that uses a slightly different syntax here:

    http://www.singulink.com/CodeIndex/post/updated-ultimate-wpf-event-method-binding

    The full code listing is available here:

    https://gist.github.com/mikernet/7eb18408ffbcc149f1d9b89d9483fc19

    Any future updates will be posted to the blog so I suggest checking there for the latest version.

    Original Answer:

    .NET 4.5+ supports markup extensions on events now. I used this to create a method binding that can be used like this:

    
    

    View model method signatures:

    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);
    }
    

    More details can be found here: http://www.singulink.com/CodeIndex/post/building-the-ultimate-wpf-event-method-binding-extension

    The full class code is available here: https://gist.github.com/mikernet/4336eaa8ad71cb0f2e35d65ac8e8e161

提交回复
热议问题