如何在向模型添加数据的同时暂停我的视图更新?

时间:2021-11-13 07:13:29

I am using a 3rd party chart component. This component is divided in 2 parts:

我正在使用第三方图表组件。该组件分为两部分:

A chart UI Component and a dataseries object that is bound to the charts dataseries property. To this data object I can add new points which are then rendered on the chart. Thats the way it is described in the documentation and it leaves me the possibility to add new points inside my ViewModel and even on a non-UI thread. It works very well and I have my View separated from my ViewModel.

图表UI组件和绑定到图表数据集属性的数据集对象。对于这个数据对象,我可以添加新的点,然后在图表上呈现。这就是文档中描述它的方式,它让我有可能在我的ViewModel中添加新点,甚至在非UI线程上。它工作得很好,我将View与ViewModel分开。

Now my problem. As a tip for performance it is recommended to call the add for new data (especially several data) the following way:

现在我的问题。作为性能提示,建议通过以下方式调用add for new data(尤其是几个数据):

using (graph.SuspendUpdate)
{
    dataseries.Add(manyPointsList);
}

But I don't have the graph inside the ViewModel. Are there any possibilities from WPF or an existing MVVM pattern to handle this?

但我在ViewModel中没有图形。 WPF或现有的MVVM模式是否有可能处理这个问题?

I have seen (couldn't find the post again):

我见过(再也找不到帖子):

using (Dispatcher.CurrentDispatcher.DisableProcessing())
{
    dataseries.Add(manyPointslist);
}

but is this really equivalent?
And wouldn't I make the assumption it is WPF? My ViewModel could be used in WinForms (in theory) and I thought the goal of MVVM is to not have View specifics in ViewModel (although disabling rendering can be seen as UI specific as well).

但这真的相当吗?我不会假设它是WPF吗?我的ViewModel可以在WinForms中使用(理论上),我认为MVVM的目标是在ViewModel中不具有View细节(尽管禁用渲染也可以被视为UI特定)。

Any thoughts on this or solution proposals?

对此或解决方案提案的任何想法?

1 个解决方案

#1


1  

I'd use Behavior for that:

我会使用行为:

public class SuspendBehavior : Behavior<THE_TYPE_OF_YOUR_CHART/GRAPH>
{
    private IDisposable disposable;

    public static readonly DependencyProperty SuspendUpdateProperty = DependencyProperty.Register(
        "SuspendUpdate", typeof(bool), typeof(SuspendBehavior), new PropertyMetadata(default(bool), OnSuspendUpdateChanged));

    public bool SuspendUpdate
    {
        get { return (bool) GetValue(SuspendUpdateProperty); }
        set { SetValue(SuspendUpdateProperty, value); }
    }

    private static void OnSuspendUpdateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var behavior = d as SuspendBehavior;

        var value = (bool) e.NewValue;
        if (value)
        {
            // AssociatedObject would be your graph
            behavior.disposable = behavior.AssociatedObject.SuspendUpdate ...
        }
        else
        {
            if (behavior.disposable != null)
                behavior.disposable.Dispose();
        }
    }
}

Attach the behavior to your chart or graph

将行为附加到图表或图表

    <i:Interaction.Behaviors>
        <local:SuspendBehavior SuspendUpdate="{Binding ShouldSuspend}"/>
    </i:Interaction.Behaviors>

and add ShouldSuspend bool property to your viewModel which will be set when you add new points

并将ShouldSuspend bool属性添加到viewModel中,该属性将在添加新点时设置

ShouldSuspend = true;
dataseries.Add(manyPointsList);
ShouldSuspend = false;

This will require you to add a reference to System.Windows.Interactivity.

这将要求您添加对System.Windows.Interactivity的引用。

although Behaviors is a concept in WPF it will only act as a code behind in your View which keeps your ViewModel clean from any references to UI elements directly.

尽管Behaviors是WPF中的一个概念,但它只能作为View中的代码,它可以使ViewModel直接清除对UI元素的任何引用。

#1


1  

I'd use Behavior for that:

我会使用行为:

public class SuspendBehavior : Behavior<THE_TYPE_OF_YOUR_CHART/GRAPH>
{
    private IDisposable disposable;

    public static readonly DependencyProperty SuspendUpdateProperty = DependencyProperty.Register(
        "SuspendUpdate", typeof(bool), typeof(SuspendBehavior), new PropertyMetadata(default(bool), OnSuspendUpdateChanged));

    public bool SuspendUpdate
    {
        get { return (bool) GetValue(SuspendUpdateProperty); }
        set { SetValue(SuspendUpdateProperty, value); }
    }

    private static void OnSuspendUpdateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var behavior = d as SuspendBehavior;

        var value = (bool) e.NewValue;
        if (value)
        {
            // AssociatedObject would be your graph
            behavior.disposable = behavior.AssociatedObject.SuspendUpdate ...
        }
        else
        {
            if (behavior.disposable != null)
                behavior.disposable.Dispose();
        }
    }
}

Attach the behavior to your chart or graph

将行为附加到图表或图表

    <i:Interaction.Behaviors>
        <local:SuspendBehavior SuspendUpdate="{Binding ShouldSuspend}"/>
    </i:Interaction.Behaviors>

and add ShouldSuspend bool property to your viewModel which will be set when you add new points

并将ShouldSuspend bool属性添加到viewModel中,该属性将在添加新点时设置

ShouldSuspend = true;
dataseries.Add(manyPointsList);
ShouldSuspend = false;

This will require you to add a reference to System.Windows.Interactivity.

这将要求您添加对System.Windows.Interactivity的引用。

although Behaviors is a concept in WPF it will only act as a code behind in your View which keeps your ViewModel clean from any references to UI elements directly.

尽管Behaviors是WPF中的一个概念,但它只能作为View中的代码,它可以使ViewModel直接清除对UI元素的任何引用。