c# - Navigating to pages with the AppBar with MvvmLight, WinRt -
i have top app bar customer user control used navigate in winrt mvvmlight application. custom control added pages
topappbar user control
<appbarbutton x:uid="hometopappbar" command="{binding homecommand}" icon="home"/> <appbarbutton x:uid="librarytopappbar" command="{binding librarycommand}" icon="library"/>
these commands added viewmodels. example,
homeviewmodel
public relaycommand librarycommand { { return new relaycommand( () => _navigationservice.navigate(typeof(librarypage))); } } public relaycommand homecommand { { return new relaycommand( () => _navigationservice.navigate(typeof(homepage))); } }
the problem i'm having if user uses top app bar, first click works (i.e., user navigated correct page.) on second click, program crashes in navigationhelper on onnavigatedfrom method (taken mvvmlight). program complains _pagekey null. there i'm doing incorrectly? how user navigate other pages app bar?
instead of adding command in appbar, decided use click event. reason why did because using command, every viewmodel uses appbar needs create command. here solution:
topappbar.xaml
<appbarbutton x:uid="hometopappbar" click="appbarbutton_home" icon="home"/> <appbarbutton x:uid="librarytopappbar" click="appbarbutton_library" icon="library"/>
topappbar.xaml.cs
private void appbarbutton_home(object sender, routedeventargs e) { simpleioc.default.getinstance<inavigationservice>().navigate(typeof(homepage)); } private void appbarbutton_library(object sender, routedeventargs e) { simpleioc.default.getinstance<inavigationservice>().navigate(typeof(librarypage)); }
any page navigated appbar (ex. homepage)
public sealed partial class homepage { public homepage() { initializecomponent(); } public homeviewmodel viewmodel { { return (homeviewmodel)datacontext; } } //needed navigation protected override void onnavigatedto(navigationeventargs e) { base.onnavigatedto(e); } //needed navigation protected override void onnavigatedfrom(navigationeventargs e) { base.onnavigatedfrom(e); } }
Comments
Post a Comment