windows phone 7 - MVVM Light - how to fire command from code behind -


I have to fire the command from WP7 applicationbar. Unfortunately, this is not possible, but Laurent has published an interesting solution:

  Private Zero ApplicationsBrarMyTimeClick (Object Sender, System.EventArgs e) {var vm = DataContext as DataViewModel; If (vm! = Null) vm.MyCommand.Execute (zero); }  

Unfortunately my code does not look at the MainViewModel class or in fact any ViewModel class! Data bindings work well, therefore ViewModel is working fine What am I doing?

Place breakpoint on that line of code and check that datacontax occurs When the breakpoint hits, if it is empty then you probably have forgotten to set the data reference in your view.

If the datacentext is not empty, make sure that this is the main type of type, or the row calling vm.MyCommand.Execute (empty) will never be called.

Depending on the code that you pasted, your idea should look like this.

  & lt; Phone: Phone Application Page: Class = "Home Page" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/ Winfx / 2006 / xaml "xmlns: phone =" clr-namespace: Microsoft.phone control, assembly = Microsoft. Phone "XMLNS: shell =" CLR-namespace: Microsoft .phone shell; assembly = Microsoft. Phone "XMLN: D = "http://schemas.microsoft.com/form / mix / 2008" xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" mc: Ignorable = "d" d: DesignWidth = "480" D: Design Hight = "768" Shell: Sy StemTray.IsVisible = "True" DataContext = "{Binding Path = Main, Source = {Static Resource Locator}}" & gt; & Lt; Grid X: Name = "Layouts" background = "Transparent" & gt; & Lt ;! - The rest have been left for simplicity - & gt; & Lt; / Grid & gt; & Lt; Phone: PhoneApplicationPage.ApplicationBar & gt; & Lt; Shell: ApplicationBar IsVisible = "True" IsMenuEnabled = "True" & gt; & Lt; Shell: ApplicationBar.MenuItems & gt; & Lt; Shell: ApplicationBarMenuItem x: name = "appBarMenuItem1" click = "ApplicationBarMenuItemClick" text = "menu item 1" /> & Lt; / Shell: ApplicationBar.MenuItems & gt; & Lt; / Shell: ApplicationBar & gt; & Lt; / Phone: PhoneApplicationPage.ApplicationBar & gt; & Lt; / Phone: PhoneApplicationPage & gt;  

Believes that your ViewModelLocator property has main type main view model .


Comments