在viewmodel中调用启动函数

时间:2016-11-02 15:09:51

标签: c# wpf mvvm viewmodel

我正在尝试在启动MVVM方式时在viewmodel中调用一个函数。我以为我说得对,但代码永远不会打到函数调用。

这是我的xaml:

<Window x:Class="TestWin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:l="clr-namespace:Timeserver"
        xmlns:viewmodel="clr-namespace:Timeserver.ViewModels"
        Title="MainWindow"
        Width="893"
        Height="Auto"
        Background="LightGray"
        ResizeMode="NoResize"
        SizeToContent="Height">
    <Window.DataContext>
        <viewmodel:MainWindowViewModel />
    </Window.DataContext>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding LoadData}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

这是我的viewmodel(必要部分):

namespace TestWin.ViewModels
{
    class MainWindowViewModel
    {
        private StructsModel model; // my model class

        private ICommand loadDataCmd;

        private ICommand showTimeWindowCmd;

        private ICommand toggleExecuteCommand { get; set; }

        private bool canExecute = true;

        public bool CanExecute
        {            
            get
            {
                return this.canExecute;
            }

            set
            {
                if (this.canExecute == value)
                {
                    return;
                }

                this.canExecute = value;
            }
        }

        public ICommand ToggleExecuteCommand
        {
            get
            {
                return toggleExecuteCommand;
            }
            set
            {
                toggleExecuteCommand = value;
            }
        }

        public ICommand ShowTimeWindowCmd
        {
            //code here
        }

        public ICommand LoadDataCmd
        {
            get
            {
                return loadDataCmd;
            }
            set
            {
                loadDataCmd = value;
            }
        }

        public void LoadData(object parameter)
        {
            model.GetData();
        }

        public MainWindowViewModel()
        {
            this.model = new StructsModel();
            LoadDataCmd = new RelayCommand(LoadData, param => this.canExecute);
            ShowTimeWindowCmd = new RelayCommand(ShowTimeWindow, param => this.canExecute);
            toggleExecuteCommand = new RelayCommand(ChangeCanExecute);
        }

        public void ShowTimeWindow(object parameter)
        {
            //code here
        }

        public void ChangeCanExecute(object obj)
        {
            canExecute = !canExecute;
        }
    }
}

有问题的功能是LoadData()。它在我的模型类中调用GetData(),我无法显示原因。不知道还有什么可以尝试。我已经看到了其他问题,我正在做同样的事情。我的其他函数ShowTimeWindow的设置方式完全相同,并且受到了影响。

2 个答案:

答案 0 :(得分:2)

如果你真的希望在Loaded上进行调用,你可以删除xaml代码

<Window.DataContext>
    <viewmodel:MainWindowViewModel />
</Window.DataContext>

并从后面的代码绑定viewmode,就像Mat上面说的那样。然后,将触发您绑定的命令(如果它与viewmodel中的命令名称相同),您将不需要在构造函数中调用vm.LoadData()。

此外,如果该命令不用于加载“已加载”数据,则CanExecute无用。

答案 1 :(得分:1)

您可以在后面的代码中创建ViewModel。比你可以调用你需要的方法。如果您想要追求卓越,可以使用Factory模式或依赖注入容器(例如Windsor)

public MainWindow()
{
    InitializeComponent();

    MainWindowViewModel vm = new MainWindowViewModel();

    DataContext = vm;

    vm.LoadData();
}
相关问题