ViewModel的DataTemplate显示缓慢

时间:2014-06-16 11:53:44

标签: wpf mvvm

我有一个简单的MainWindowView

<Window ...>
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:ClassListForTeacherViewModel}">
            <local:ClassListForTeacher />
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="109*"/>
            <ColumnDefinition Width="408*"/>
        </Grid.ColumnDefinitions>
        <local:NavigationControl Grid.Column="0"
                                 VerticalAlignment="Stretch"
                                 HorizontalAlignment="Stretch"
                                 Width="Auto"/>
        <ContentControl Content="{Binding MainView}"
                        Grid.Column="1"
                        VerticalAlignment="Stretch"
                        HorizontalAlignment="Stretch"
                        Width="Auto"/>
    </Grid>
<Window>

和MainWindowViewModel

public class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel(string name)
        : this(name, null)
    {
    }

    public MainWindowViewModel(string name, ViewModelBase mainView)
    {
        Name = name;
        MainView = mainView;
        NavigateToTeacherClassesCommand = new DelegateCommand(o => NavigateToTeacherClasses());
    }
    //
    // Properties
    //
    public string Name { get; private set; }

    private ViewModelBase _MainView;
    public ViewModelBase MainView
    {
        get 
        {
            return _MainView;
        }
        private set
        {
            _MainView = value;
            RaisePropertyChangedEvent("MainView");
        }
    }

    public ICommand NavigateToTeacherClassesCommand { get; private set; }
    //
    // Functions
    //
    private void NavigateToTeacherClasses()
    {
        MainView = new ClassListForTeacherViewModel();
    }
}

ClassListForTeacher视图仅包含没有逻辑的ListView控件(尚未)。当我单击MainWindowView中NavigationControl中的Label时,ClassListForTeacher在MainWindowView的MainView属性上设置,并通过DataTemplate显示。这是准系统计划;还没有加载数据。我的问题是显示需要太长时间,也许大约需要3秒钟。这是我第一次尝试MVVM,我看到这是一种常见的技术(使用DataTemplate),这是正常的吗?

我还注意到ClassListForTeacher的ViewModel被调用了两次,但我没有其他代码调用它。一个来自NavigateToTeacherClasses函数,另一个我不知道。当WPF实际解析DataTemplate(这是一个UserControl)时,是否再次调用构造函数?如果是这样,我该如何防止这种情况?

修改

NavigationControl的XAML

<UserControl...    
<StackPanel>
    <Expander Header="Grading" IsExpanded="True">
        <StackPanel Background="#FFE5E5E5">
            <Label Content="My Classes">
                <Label.InputBindings>
                    <MouseBinding Command="{Binding NavigateToTeacherClassesCommand}" MouseAction="LeftClick" />
                </Label.InputBindings>
            </Label>
        </StackPanel>
    </Expander>
</StackPanel>
</UserControl>

ClassListForTeacher的XAML:

<UserControl... 
    <Grid x:Name="LayoutRoot">
        <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</UserControl>

0 个答案:

没有答案