gpt4 book ai didi

wpf - 使用模型优先方法时是否可以缓存 View ?

转载 作者:行者123 更新时间:2023-12-04 17:41:02 25 4
gpt4 key购买 nike

在我们的产品中,我们使用 MVVM 模型优先方法,它运行良好,但有一个警告。当 View 变得复杂时,从数据模板创建它需要时间。如果 View 频繁地显示和隐藏,它会变得有点烦人。如果首先使用 View ,那么在需要时缓存 View 会很容易——但是当首先使用 DataTemplate 和模型时,我们对 View 创建没有太多控制。
有人在没有切换到 View 优先方法的情况下解决了这个问题吗?

最佳答案

如果使用@blindmeis 想法,效果会很好。

整体配方:

创建一个名为 ViewCache 的 ContentControl 或 UserControl:

public partial class ViewCache
{
public ViewCache()
{
InitializeComponent();
Unloaded += ViewCache_Unloaded;
}

void ViewCache_Unloaded(object sender, RoutedEventArgs e)
{
Content = null;
}

private Type _contentType;
public Type ContentType
{
get { return _contentType; }
set
{
_contentType = value;
Content = ViewFactory.View(value); // use you favorite factory
}
}
}

在DataTemplate中,使用ViewCache,传递你要使用的真实 View 的类型:
<Window.Resources>
<DataTemplate DataType="{x:Type TestViewCache:Foo}">
<TestViewCache:ViewCache ContentType="{x:Type TestViewCache:View }"/>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ContentPresenter Height="200" Width="300"
Content="{Binding ViewModel}"/>
<Button Content="Set VM" Click="SetVMClick"/>
<Button Content="UnSet VM" Click="UnSetVMClick"/>
</StackPanel>

关于wpf - 使用模型优先方法时是否可以缓存 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3877611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com