简体   繁体   English

WP8列表框加载的MVVM

[英]WP8 Listbox Loaded MVVM

I'm making a WP8 application using the MVVM patern (Caliburn.Micro). 我正在使用MVVM模式(Caliburn.Micro)制作WP8应用程序。

I'm using a ListBox Named ProgramsList and I want to do something when Loaded. 我正在使用一个名为ProgramsList的列表框,并且想在加载时做一些事情。

<ListBox Name="ProgamsList" ItemsSource="{Binding ProgramsList}" HorizontalAlignment="Stretch" FontFamily="Portable User Interface" Loaded="">

When not using the MVVM patern I could use the automatic generated Event handler. 当不使用MVVM模式时,可以使用自动生成的事件处理程序。

How can I do this the right way using the MVVM patern? 如何使用MVVM模式正确执行此操作?

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"

<ListBox Name="ProgamsList" ItemsSource="{Binding ProgramsList}" HorizontalAlignment="Stretch" FontFamily="Portable User Interface" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <cmd:EventToCommand Command="{Binding LoadedCommand}" />
        </i:EventTrigger>        
    </i:Interaction.Triggers>
</ListBox>


    public RelayCommand LoadedCommand
            {
                get;
                private set;
            }

            /// <summary>
            /// Initializes a new instance of the SplashScreenViewModel class.
            /// </summary>
            public SplashScreenViewModel()
            {
                LoadedCommand = new RelayCommand(toDoSomehing);
            }

    private void toDoSomething(){
    }

You can use commands to expose your logic from the ViewModel an then use behaviors, for example: http://metroeventtocommand.codeplex.com/ 您可以使用命令从ViewModel公开逻辑,然后使用行为,例如: http : //metroeventtocommand.codeplex.com/

If that doesn't fit your needs, you can always use an event handler and call the command from there. 如果这不符合您的需求,则可以始终使用事件处理程序并从那里调用命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM