Caliburn.Micro support for PasswordBox? -
the caliburn.micro home page @ http://caliburnmicro.com makes below claim unable make cm work passwordbox control using variation can think of example. don't see how work anyway since names not same case. have cm example allow me value of passwordbox? there particular version of cm required? i'm running version 1.5.2 of cm. ideally w/o using attached properties if can work cm , way fine. please no lectures on security issues not issue in case.
apply methods between view , view model automatically parameters , guard methods
<stackpanel> <textbox x:name="username" /> <passwordbox x:name="password" /> <button x:name="login" content="log in" /> public bool canlogin(string username, string password) { return !string.isnullorempty(username) && !string.isnullorempty(password); }
public string login(string username, string password) { ... }
i've been able work dependency properties, bypassing convention binding goodness caliburn.micro supplies. recognize that's not ideal, pragmatically solution regularly use. believe when hit snag historically, found this post on stackoverflow led me in direction. consideration:
public class boundpasswordbox { private static bool _updating = false; /// <summary> /// boundpassword attached dependency property /// </summary> public static readonly dependencyproperty boundpasswordproperty = dependencyproperty.registerattached("boundpassword", typeof(string), typeof(boundpasswordbox), new frameworkpropertymetadata(string.empty, onboundpasswordchanged)); /// <summary> /// gets boundpassword property. /// </summary> public static string getboundpassword(dependencyobject d) { return (string)d.getvalue(boundpasswordproperty); } /// <summary> /// sets boundpassword property. /// </summary> public static void setboundpassword(dependencyobject d, string value) { d.setvalue(boundpasswordproperty, value); } /// <summary> /// handles changes boundpassword property. /// </summary> private static void onboundpasswordchanged( dependencyobject d, dependencypropertychangedeventargs e) { passwordbox password = d passwordbox; if (password != null) { // disconnect handler while we're updating. password.passwordchanged -= passwordchanged; } if (e.newvalue != null) { if (!_updating) { password.password = e.newvalue.tostring(); } } else { password.password = string.empty; } // now, reconnect handler. password.passwordchanged += passwordchanged; } /// <summary> /// handles password change event. /// </summary> static void passwordchanged(object sender, routedeventargs e) { passwordbox password = sender passwordbox; _updating = true; setboundpassword(password, password.password); _updating = false; } } then, in xaml:
<passwordbox pwbx:boundpasswordbox.boundpassword="{binding userpassword, mode=twoway,updatesourcetrigger=propertychanged,notifyonvalidationerror=true,validatesondataerrors=true}" /> and pwbx found namespace on window tag:
<window x:class="myproject.views.loginview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:ignorable="d" xmlns:pwbx="clr-namespace:myproject.client.controls"> the viewmodel:
using caliburn.micro; using myproject.core; using myproject.repositories; using myproject.types; using myproject.viewmodels.interfaces; namespace myproject.viewmodels { public class loginviewmodel : screen, iloginviewmodel { private readonly iwindowmanager _windowmanager; private readonly iunitrepository _unitrepository; public bool isloginvalid { get; set; } public unit loggedinunit { get; set; } private string _password; public string userpassword { { return _password; } set { _password = value; notifyofpropertychange(() => userpassword); notifyofpropertychange(() => canlogin); } } private string _name; public string username { { return _name; } set { _name = value; notifyofpropertychange(() => username); notifyofpropertychange(() => canlogin); } } public loginviewmodel(iwindowmanager windowmanager,iunitrepository unitrepository) { _windowmanager = windowmanager; _unitrepository = unitrepository; displayname = "myproject - login"; version = applicationversionrepository.getversion(); } public string version { get; private set; } public void login() { // login logic var credentials = new usercredentials { username = username, password=userpassword }; var resp = _unitrepository.authenticateunit(credentials); if (resp == null) return; if (resp.isvalid) { isloginvalid = true; loggedinunit = resp.unit; tryclose(); } else { var dialog = new messageboxviewmodel(dialogtype.warning, dialogbutton.ok, "login failed", "login error: " + resp.invalidreason); _windowmanager.showdialog(dialog); } } public bool canlogin { { return !string.isnullorempty(username) && !string.isnullorempty(userpassword); } } } }
Comments
Post a Comment