Thursday 15 July 2010

c# - Page Navigation using MVVM in Store App -



c# - Page Navigation using MVVM in Store App -

i'm having serious headache problem. dislike store apps forced utilize in case. i've worked xaml few weeks.

my question is: how can phone call relaycommand in viewmodel (from view of course) alter page on view? , better, alter using uri, can pass command parameter file.

i'm totally lost on this. i'm using this.frame.navigate(type type) in view code behind navigate through pages.

i , mean appreciate description z on in case.

i presume building framecontainer on view , send viewmodel , there navigate current frame another. i'm not sure how works in store apps.

i sorry lack of questions, i'm on deadline , need view connected viewmodel in proper way.. don't having both view codebehind viewmodel code.

there 2 ways this, simple way pass relay command action view view model.

public mainpage() { var vm = new myviewmodel(); vm.gotopage2command = new relaycommand(()=>{ frame.navigate(typeof(page2)) }); this.datacontext = vm; } <button command={binding gotopage2command}>go page 2</button>

another way using ioccontainer , dependencyinjection. 1 more losely coupled approach.

we need interface navigation page don't need reference or know pagex or ui element assuming viewmodel in separate project doesn't know ui.

viewmodel project:

public interface inavigationpage { type pagetype { get; set; } } public interface inavigationservice { void navigate(inavigationpage page) { get; set; } } public class myviewmodel : viewmodelbase { public myviewmodel(inavigationservice navigationservice, inavigationpage page) { gotopage2command = new relaycommand(() => { navigationservice.navigate(page.pagetype); }) } private icommand gotopage2command { get; private set; } }

ui project:

public class navigationservice : inavigationservice { //assuming navigate in root frame frame navigationframe = window.current.content frame; public void navigate(inavigationpage page) { navigationframe.navigate(page.pagetype); } } public abstract class navigationpage<t> : inavigationpage { public navigationpage() { this.pagetype = typeof(t); } } public class navigationpage1 : navigationpage<page1> { } public class mainpage : page { public mainpage() { //i'll place container logic here, can place in bootstrapper or in app.xaml.cs if want. var container = new unitycontainer(); container.registertype<inavigationpage, navigationpage1>(); container.registertype<inavigationservice, navigationservice>(); container.registertype<myviewmodel>(); this.datacontext = container.resolve<myviewmodel>(); } }

c# wpf xaml mvvm windows-store-apps

No comments:

Post a Comment