Better way to trigger OnPropertyChanged

后端 未结 6 1255
悲哀的现实
悲哀的现实 2020-11-29 04:51

We have a WPF Project that follows the MVVM pattern.

In the View Model there is a lot of code that looks like this:

    private string m_Fieldname;
          


        
6条回答
  •  误落风尘
    2020-11-29 05:29

    Ok this doesn't clean the code up but it shortens the amount of time writing all of this code. I can now blow through a list of 20+ properties in a couple of minutes.

    First you need to define all of your private variables, I'm assuming your first character is a lower case. Now copy that those variables into another list as the macro removes the original line.

    For example:

    private int something1 = 0;
    private int something2 = 0;
    private int something3 = 0;
    private int something4 = 0;
    private int something5 = 0;
    private int something6 = 0;
    

    Then put your cursor somewhere on that line and run this macro. Again this replaces the line with a public property so make sure you have the same private member variable defined before this in your class.

    I'm sure this script could be cleaned up, but it saved me hours of tedious work today.

    Sub TemporaryMacro()
        DTE.ActiveDocument.Selection.StartOfLine(VsStartOfLineOptions.VsStartOfLineOptionsFirstText)
        DTE.ActiveDocument.Selection.Delete(7)
        DTE.ActiveDocument.Selection.Text = "public"
        DTE.ActiveDocument.Selection.CharRight()
        DTE.ExecuteCommand("Edit.Find")
        DTE.Find.FindWhat = " "
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.Backwards = False
        DTE.Find.MatchInHiddenText = False
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.Action = vsFindAction.vsFindActionFind
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.ActiveDocument.Selection.CharRight()
        DTE.ActiveDocument.Selection.WordRight(True)
        DTE.ActiveDocument.Selection.CharLeft(True)
        DTE.ActiveDocument.Selection.Copy()
        DTE.ActiveDocument.Selection.CharLeft()
        DTE.ActiveDocument.Selection.CharRight(True)
        DTE.ActiveDocument.Selection.ChangeCase(VsCaseOptions.VsCaseOptionsUppercase)
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.StartOfLine(VsStartOfLineOptions.VsStartOfLineOptionsFirstText)
        DTE.ExecuteCommand("Edit.Find")
        DTE.Find.FindWhat = " = "
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.Backwards = False
        DTE.Find.MatchInHiddenText = False
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.Action = vsFindAction.vsFindActionFind
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.ActiveDocument.Selection.CharLeft()
        DTE.ActiveDocument.Selection.EndOfLine(True)
        DTE.ActiveDocument.Selection.Delete()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "get { return "
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.Text = "; }"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "set"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "if("
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.Text = " != value)"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.Text = " = value;"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "OnPropertyChanged("""
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.Text = """);"
        DTE.ActiveDocument.Selection.StartOfLine(VsStartOfLineOptions.VsStartOfLineOptionsFirstText)
        DTE.ExecuteCommand("Edit.Find")
        DTE.Find.FindWhat = """"
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.Backwards = False
        DTE.Find.MatchInHiddenText = False
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.Action = vsFindAction.vsFindActionFind
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.ActiveDocument.Selection.CharRight()
        DTE.ActiveDocument.Selection.CharRight(True)
        DTE.ActiveDocument.Selection.ChangeCase(VsCaseOptions.VsCaseOptionsUppercase)
        DTE.ActiveDocument.Selection.Collapse()
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.LineDown()
        DTE.ActiveDocument.Selection.EndOfLine()
    End Sub
    

提交回复
热议问题