Dialog with button for selecting element

For the record, tried a variation of a given example, and again it worked only for first click and then sent a similar exception.

Example that I was not able to re run:
[Select model element from UI upon button click]

The code I used:
from Autodesk.Revit.UI.Selection import ISelectionFilter
from Autodesk.Revit.UI.Selection import ObjectType

gdict = globals()
uiapp = revit
uidoc = uiapp.ActiveUIDocument
xamlfile = script.get_bundle_file(‘dialog/dialog.xaml’)

class CustomISelectionFilter(ISelectionFilter):
def init(self, category_name):
self.category_name = category_name

def AllowElement(self, e):
return True

def AllowReference(self, ref, point):
return True

class MyWindow(Windows.Window):
def init(self):
# self is the instance of the class and we are loading xaml file into that instance
wpf.LoadComponent(self, xamlfile)

def step(self, sender, args):
pass

def parseOptions(self):
pass

def clear(self, sender, args):
pass

def TextBox_KeyDown(self, sender, args):
pass

def handle_command_string(self, sender, args):
pass

def dev(self, sender, args):
try:
self.Hide()
Pipe_Sel = uidoc.Selection.PickObject(ObjectType.Element, CustomISelectionFilter(“Pipes”), “Select a Pipe”)
self.Show()

except Exception:
  print("Operation canceled", "Canceled by the user")

def build(self, sender, args):
pass

MyWindow().ShowDialog()