c# - Sqlite Data not displaying -
application x:class="sqlasynctest.app" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:sqlasynctest" xmlns:viewmodel="using:sqlasynctest.viewmodel" requestedtheme="light"> <application.resources> <resourcedictionary> <viewmodel:viewmodellocator x:key="locator" /> </resourcedictionary> </application.resources>
public class viewmodellocator { public viewmodellocator() { servicelocator.setlocatorprovider(() => simpleioc.default); if (!simpleioc.default.isregistered<idal>()) { simpleioc.default.register<idal, dal>(); } simpleioc.default.register<mainviewmodel>(); } public mainviewmodel main { { return servicelocator.current.getinstance<mainviewmodel>(); } } public static void cleanup() { // todo clear viewmodels } }
mainviewmodel
public class mainviewmodel : viewmodelbase { private readonly idal _idal; private ilist<user> _user; public ilist<user> users { { return _user; } set { set("users", ref _user, value); } } public mainviewmodel(idal dal) { _idal = dal; loaddatacommand = new relaycommand(async () => await loaddata()); } public icommand loaddatacommand { get; private set; } public async task loaddata() { users = await _idal.loadalluser(); } }
mainpage
<page x:class="sqlasynctest.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:sqlasynctest" datacontext="{binding main, source={staticresource locator}}" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d"> <grid background="{themeresource applicationpagebackgroundthemebrush}"> <grid.columndefinitions> <columndefinition width="*"/> <columndefinition width="20"/> <columndefinition width="*"/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="140"/> <rowdefinition height="*"/> </grid.rowdefinitions> <listbox grid.column="0" grid.row="1" selectionmode="single" itemssource="{binding users}" margin="120,0,0,40"> <listbox.itemtemplate> <datatemplate> <stackpanel orientation="horizontal"> <textblock text="{binding firstname}"/> <textblock text="{binding lastname}"/> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox> </grid>
table , methods fine think binding wrong , me loaddata() method not called .
i got solution
protected override void onnavigatedto(navigationeventargs e) { var viewmodel = (mainviewmodel)datacontext; if (viewmodel != null) { viewmodel.loaddata(); }
i don't touch mainpage.cs other way in xaml code
you have loaddatacommand
in view model, not called anywhere. there no magic here – have invoke either xaml, or code-behind file. here's way can done in xaml:
- install
microsoft.xaml.behaviors.uwp.managed
nuget package. modify xaml of main page:
<page ... xmlns:i="using:microsoft.xaml.interactivity" xmlns:c="using:microsoft.xaml.interactions.core"> <i:interaction.behaviors> <c:eventtriggerbehavior eventname="loaded"> <c:invokecommandaction command="{binding loaddatacommand}"/> </c:eventtriggerbehavior> </i:interaction.behaviors> <grid ...>
remove overridden
onnavigatedto
method code-behind file.loaddatacommand
invoked each timemainpage
loaded.
Comments
Post a Comment