c# - How to update an image on a page in UWP? -
i've got gridviewitem. gridviewitem has background imagebrush. want change imagebrush new source when clicking button.
for i'm using:
blck.background = new imagebrush(new bitmapimage(new uri("ms-appx:///assets/sensorbg.png")));
it work new image shows whenever click on corresponding gridviewitem. can tell me how update without having click on gridviewitem?
i've tried put within block no success:
coreapplication.mainview.corewindow.dispatcher.runasync(coredispatcherpriority.normal, () => { blck.background = new imagebrush(new bitmapimage(new uri("ms-appx:///assets/sensorbg.png"))); } );
the best be, if have defined itemclass suitable properties , implement inotifypropertychanged - appropriate binding, every change update ui. here small sample - xaml:
<stackpanel> <button content="change background of second item" click="button_click"/> <gridview background="{themeresource applicationpagebackgroundthemebrush}" itemssource="{x:bind items}"> <gridview.itemtemplate> <datatemplate x:datatype="local:itemclass"> <border> <border.background> <imagebrush imagesource="{x:bind image, mode=oneway}"/> </border.background> <textblock text="{x:bind name}"/> </border> </datatemplate> </gridview.itemtemplate> </gridview> </stackpanel>
and code behind:
public sealed partial class mainpage : page { public list<itemclass> items = new list<itemclass>(); public mainpage() { items.add(new itemclass { name = "first item", image = new bitmapimage(new uri("ms-appx:///assets/storelogo.png")) }); items.add(new itemclass { name = "second item", image = new bitmapimage(new uri("ms-appx:///assets/storelogo.png")) }); this.initializecomponent(); } private void button_click(object sender, routedeventargs e) => items[1].image = new bitmapimage(new uri("ms-appx:///test.jpg")); } public class itemclass : inotifypropertychanged { public event propertychangedeventhandler propertychanged; private void raiseproperty(string name) => propertychanged?.invoke(this, new propertychangedeventargs(name)); private imagesource image; public imagesource image { { return image; } set { image = value; raiseproperty(nameof(image)); } } public string name { get; set; } }
Comments
Post a Comment