Is a dispatcher needed to change data-bound properties in WPF .Net Core 3 or newer?

我的未来我决定 提交于 2020-04-30 06:28:40

问题


A lot of examples (even from MS) use a dispatcher to update data-bound properties and a bunch of different answers could be found. Does an 'official' statement exist? Currently, I always use a dispatcher and I would only change this if I can be sure that this is an official feature and it will still work on future .Net versions.


回答1:


I don't believe an official statement exists. However, it generally seems to depend on the type of update that you're doing. If you're updating a normal property on a VM, then it doesn't matter, if you're updating a dependency property then it does. If you're adding or removing from a list then it doesn't matter, if you're adding or removing from an observable collection then it does.

Generally, I'd say avoid dependency properties in VM's and use INotifyPropertyChanged, leave them to your UserControls. Then you don't have to worry about them. Add a method like DoPropertyChanged(propertyname) to your base class for your VM's so that you can then create a property snippet like VB.net

Private _MajorChange As String
    Public Property MajorChange As String
        Get
            Return _MajorChange
        End Get
        Set(value As String)
            _MajorChange = value
            DoPropertyChanged("MajorChange")
        End Set
    End Property

C#

    private string _MajorChange;
    public string MajorChange
    {
        get
        {
            return _MajorChange;
        }
        set
        {
            _MajorChange = value;
            DoPropertyChanged("MajorChange");
        }
    }

and quickly add properties to your VM's.

We also make heavy use of index properties and have a snippet for that too. VB

    Public Property PropertyName As String
        Get
            Return Me("ColumnName")
        End Get
        Set(value As String)
            Me("ColumnName") = value
            DoPropertyChanged("PropertyName")
        End Set
    End Property

C#

public string PropertyName
{
    get
    {
        return <string>this("ColumnName");
    }
    set
    {
        this("ColumnName") = value;
        DoPropertyChanged("PropertyName");
    }
}



回答2:


Is a dispatcher needed to change data-bound properties in WPF .Net Core 3 or newer?

No. It's not needed in earlier versions either, nor on .NET Framework versions.

WPF has always automatically marshalled property-change updates to the UI thread as needed, for any properties bound to a dependency property via the normal data binding mechanisms (e.g. {Binding} markup in XAML).

I don't know that this is documented explicitly. I looked around on the Microsoft Docs site, on the most likely topics, such as the data-binding and INotifyPropertyChanged pages for WPF, but didn't see anything. However, it's a well-known feature of WPF, and is mentioned in this 2014 article from MSDN: MVVM : Multithreading and Dispatching in MVVM Applications:

…WPF automatically dispatches the PropertyChanged event to the main thread…

See also e.g. Task parallel library INotifyPropertyChanged NOT throwing an exception?

Note that this is only for simple single-value properties. Collections are a different matter, though WPF since 4.5 has also included some support for automatic marshaling of collection-changed events. See e.g. How do I update an ObservableCollection via a worker thread?



来源:https://stackoverflow.com/questions/60972903/is-a-dispatcher-needed-to-change-data-bound-properties-in-wpf-net-core-3-or-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!