Is it acceptable to put code to load an image into an Image control in the code behind file of a View in WPF MVVM? -
i unclear conventions should follow in regards code behind file of view in wpf mvvm.
in specific scenario, want know whether considered practice place code in code behind, linked click event of button on view, locate image hard drive , load image command on view. code this:
var ofd = new openfiledialog { filter = "bitmap files(*.bmp)|*.bmp" }; ofd.showdialog(); if (ofd.filename != null) { var image = bitmapfactory.converttopbgra32format(new writeablebitmap(new bitmapimage(new uri(ofd.filename, urikind.absolute)))); myimagecontrol.width = image.width; myimagecontrol.height = image.height; myimagecontrol.source = image; }
is much logic code-behind of view? if should place in viewmodel or in different class , phone call method view?
also, on broader scale, great if provide link definitive guidelines regarding considered acceptable set in code-behind of view, , not.
thanks
first of all, it's of import remember mvvm is, said, set of guidelines not rules. , can little sketchy should where...
that said, in illustration have quite lot of logic not ui-specific logic.
loading image hard drive should done in viewmodel. image should set on property in viewmodel. then, image command in view should bind imagesource
said property.
about dialog (e.g. openfiledialog) - there 3 valid (imho) ways can done.
first create 'filechoosingservice' called viewmodel and, in turn, raises openfiledialog , returns filename.
second alternative open dialog view, , set result property in viewmodel. viewmodel see property has changed , in turn load image harddrive.
third alternative second, instead of setting property, have view invoke loadimagecommand
in viewmodel , pass commandparameter
filename.
wpf mvvm standards
No comments:
Post a Comment