询问者
WPF动态加载页面会卡顿

问题
-
我使用的是mvvm模式,在viewmodel中创建了一个字典,把我软件中几个主要的界面(usercontrol格式)new出来存到了字典中,界面放了一个ContentControl,Content属性与后台绑定,这样,当点击界面上按钮时,我会从字典中找到对应的页面然后传给ContentControl的Content。
不过在我界面开始变得稍微复杂之后,每次切换界面就开始出现卡顿,复杂的界面切换时,会卡顿半秒钟以上,有没有什么好的办法可以解决这个问题?
我试过fream,tabcontrol,只有很小的提升。
当直接写tabcontrol时,即使界面很复杂,也可以瞬间切换,但是绑定的就很卡
全部回复
-
你好,
根据你的描述,我做了一个简单的测试,并没有重现你的问题,以下是demo的代码:
public class ViewModel : INotifyPropertyChanged { Dictionary<string, string> pairs = new Dictionary<string, string>(); public Dictionary<string, string> Pairs { get { return pairs; } set { pairs = value; NotifyPropertyChanged("Pairs"); } } public ViewModel() { Pairs.Add("UserControl1", "MyUserCtrs\\UserControl1.xaml"); Pairs.Add("UserControl2", "MyUserCtrs\\UserControl2.xaml"); Pairs.Add("UserControl3", "MyUserCtrs\\UserControl3.xaml"); } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
MainWindow页面代码是:
<Window.DataContext> <local:ViewModel></local:ViewModel> </Window.DataContext> <ContentControl> <TabControl ItemsSource="{Binding Pairs}"> <TabControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Key}"/> </DataTemplate> </TabControl.ItemTemplate> <TabControl.ContentTemplate> <DataTemplate > <Frame Source="{Binding Value}"></Frame> </DataTemplate> </TabControl.ContentTemplate> </TabControl> </ContentControl>
请查看一下,是否与你的代码类似,如果完全不同,你可以提供你的sample供我测试分析吗?
谢谢
Daisy Tian
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com. -
要不要尝试先将页面加载后在传递绑定内容?
你的绑定内容是否递归的内容?
你可以参考一下
界面和你一样都是放置contencontrol,不过将页面作为datatemplate而已,数据作为content。
我将代码传到了码云 你可以看一下
-
要试试先加载页面,在传递绑定内容嘛?