Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows phone 7 MVVM指示灯-如何从代码隐藏触发命令_Windows Phone 7_Mvvm Light - Fatal编程技术网

Windows phone 7 MVVM指示灯-如何从代码隐藏触发命令

Windows phone 7 MVVM指示灯-如何从代码隐藏触发命令,windows-phone-7,mvvm-light,Windows Phone 7,Mvvm Light,我需要从WP7应用程序栏启动命令。不幸的是,这是不可能的,但Laurent发布了有趣的解决方法: private void ApplicationBarMenuItemClick(object sender, System.EventArgs e) { var vm = DataContext as MainViewModel; if (vm != null) vm.MyCommand.Execute(null); } 不幸的是,我的代码隐藏根本看不到MainViewMode

我需要从WP7应用程序栏启动命令。不幸的是,这是不可能的,但Laurent发布了有趣的解决方法:

private void ApplicationBarMenuItemClick(object sender, System.EventArgs e)
{
  var vm = DataContext as MainViewModel;
  if (vm != null)
     vm.MyCommand.Execute(null);
}

不幸的是,我的代码隐藏根本看不到
MainViewModel
类或任何
ViewModel
类!数据绑定工作正常,因此
ViewModel
工作正常。我做错了什么?

在该行代码上放置一个断点,并检查当遇到断点时,DataContext的值是多少。如果为null,则可能忘记在视图中设置数据上下文

如果DataContext不为null,请确保它的类型为MainViewModel,否则将永远不会调用调用vm.MyCommand.Execute(null)的行

根据粘贴的代码,视图应该如下所示

<phone:PhoneApplicationPage x:Class="MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    shell:SystemTray.IsVisible="True"
    DataContext="{Binding Path=Main, Source={StaticResource Locator}}"
    >

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <!-- the rest has been ommitted for simplicity -->
    </Grid>

    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem x:Name="appBarMenuItem1" Click="ApplicationBarMenuItemClick" Text="Menu Item 1" />
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>


这假设您的ViewModelLocator具有类型为MainViewModel的属性Main,在该代码行上放置一个断点,并在命中断点时检查DataContext的值。如果为null,则可能忘记在视图中设置数据上下文

如果DataContext不为null,请确保它的类型为MainViewModel,否则将永远不会调用调用vm.MyCommand.Execute(null)的行

根据粘贴的代码,视图应该如下所示

<phone:PhoneApplicationPage x:Class="MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    shell:SystemTray.IsVisible="True"
    DataContext="{Binding Path=Main, Source={StaticResource Locator}}"
    >

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <!-- the rest has been ommitted for simplicity -->
    </Grid>

    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem x:Name="appBarMenuItem1" Click="ApplicationBarMenuItemClick" Text="Menu Item 1" />
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>


这假设您的ViewModelLocator具有类型为MainViewModel

的属性Main,实际上问题要容易得多。我查看了一些示例,发现了问题所在-在页面代码中,您需要添加viewmodel名称空间!简单!事实上,这个问题要容易得多。我查看了一些示例,发现了问题所在-在页面代码中,您需要添加viewmodel名称空间!简单!