c# - MVVM Violations -
c# - MVVM Violations -
i want clear issues mvvm violation.becuase of i've created solution projects demonstrate cases. here's definition (projects) of solution :
view (its wpf class libraray , has views) viewmodel (its class libraray , has viewmodels ) model (its class libraray , has models) domain (its class libraray , has application datamodels ) core (its class libraray , has core of wpf relaycommnd or eventtocommand) application ( wpf application , startup project) externalcustomcontrol (its wpf custom command library created imaginary 3rd party company)i offer download whole solution understand improve here
first issue : i've eventtocommand in mainwindow.xaml closing event of window , attached mainwindowclosingcommand passeventargstocommand set true,then,in mainviewmodel there's handler command named onmainwindowclosing
private void onmainwindowclosing(object parameter) { var arg = parameter canceleventargs; // best way show message dialog user? // have send message view show messagebox dialog , window send me reply continue? // imessageboxservice? doesn't violates mvvm? // doesn't next code violates mvvm? // cancel closing of window isnt ui side duty? arg.cancel = true; }
and totally whenever want set e.handled or e.cancel face issue.so know other way doesn't need cast parameter canceleventargs ?
second issue : i've eventtocommand in mainwindow.xaml previewmousedown event of grid , attached mouseclickcommand passeventargstocommand set true,then,in mainviewmodel there's handler command named onmouseclick:
private void onmouseclick(object parameter) { // var arg = parameter mousebuttoneventargs; // violation of mvvm : cast parameter mousebuttoneventargs have add together refrence // presentationcore.dll in viewmodel project // next , worse step in cases need know original source of event // (maybe stackpanel or label or etc) , 1 time again vioaltes mvvm // whats workaround? }
third issue : used thirdparty control(imagine infragistics or devexpress or other 3rd party command here illustration created imaginary command in solution externalcustomcontrol project) in mainwindow :
<thirdparty:thirdpartycustomcontrol grid.row="1" itemssource="{binding myitemssource,converter={staticresource converterkey}}" />
and thirdpartycustomcontrol has property of type ienumarabe<customcontroldatamodel>
(customcontroldatamodel type exists in externalcustomcontrol assembly) know if want create property in mainviewmodel command type customcontroldatamodel have add together refrence externalcustomcontrol.dll in viewmodel project , violates mvvm created type named mydatamodel , bound itemssource of command myitemssource property in mainviewmodel :
// if define myitemssource list<customcontroldatamodel> have add together refrence externalcustomcontrol.dll // , think 1 time again violate mvvm (because externalcustomcontrol.dll ui side controls assembly) public list<mydatamodel> myitemssource { get; set; }
so bound property of type customcontroldatamodel property of type mydatamodel , of course of study need converter :
public object convert(object value, type targettype, object parameter, c system.globalization.cultureinfo culture) { // imagine when source info (mydatamodel) huge (for illustration 1 milion) (this dummy conversion) // affects performance if (value list<mydatamodel>) { var result = new list<customcontroldatamodel>(); (value list<mydatamodel>).foreach(myval => { var custdatamodel = new customcontroldatamodel(); custdatamodel.id = myval.id; custdatamodel.name = myval.name; custdatamodel.age = myval.age; result.add(custdatamodel); }); homecoming result; } homecoming value; }
and question know improve way dummy conversion or add together 3rd party assemblies view , viewmodel both?
these issues i've faced , i'll appreciated if add together more if know other issues , share expertise everyone.
upadte:
for messagebox part of first issue suggest link mesagebox
thanks.
my closing code looks this, dont think violating mvvm
xaml
<window> <i:interaction.triggers> <i:eventtrigger eventname="closing"> <cmd:eventtocommand command="{binding closingcommand}" passeventargstocommand="true" /> </i:eventtrigger> </i:interaction.triggers>
mainviewmodel.cs
public icommand closingcommand { { homecoming this._closingcommand ?? (this._closingcommand = new delegatecommand<canceleventargs>((args) => { //i set property in app.xaml.cs when shut downwards app there //application.current.shutdown(); if (app.isshutdown) return; if (this.haschanges) { var result = _msgservice.show("blup blup", "blup", messageboxbutton.yesno, messageboximage.question, messageboxresult.no); if (result == messageboxresult.no) { args.cancel = true; } } else { var result = messagebox.show("blup blup", "blup", messageboxbutton.yesno, messageboximage.question, messageboxresult.no); if (result == messageboxresult.no) { args.cancel = true; } } })); } }
third issue (thirdparty control): dont problem if command need type of collection expose these colelction through vm.
second issue: hard say. utilize this, , mvvm ;)
<datagrid x:name="mygrd"> <i:interaction.triggers> <i:eventtrigger eventname="mousedoubleclick"> <commanding:eventtocommand command="{binding path=opencommand}" commandparameter="{binding elementname=mygrd, path=selecteditem}"/> </i:eventtrigger> </i:interaction.triggers>
and @ end mvvm in mind things simple
c# wpf design-patterns mvvm
Comments
Post a Comment