简体   繁体   中英

PropertyChanged is not fired from a class in a different assembly

I have a class library with a base class ( BaseViewModel ) that implements INotifyPropertyChanged , and a class that derive from it ( TestExternal ).

I use Fody v4.2.1 and PropertyChanged.Fody v2.6.1.

In the WPF app, I use that class as DataContext. When a property is changed, it is not reflected in the app. However, if I copy (and rename to TestInternal ) that class from the class library to the app, property changes are reflected in the app. The class TestInternal is derived from the very same BaseViewModel in the class library.

The class in this simplified example consists of a string and ObservableCollection<string> .

The ObservableCollection<string> is bound to a control, and adding the element "d" is reflected correctly in the app in both cases. But setting the string property to "C" is only reflected in the TestInternal class.

What do I need to do to get this working?

BaseViewModel.cs

// This class is in the class library
public class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };

    public void OnPropertyChanged(string name)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

TestExternal.cs

// This class is in the class library
public class TestExternal : BaseViewModel
{
    public ObservableCollection<string> UserProjects { get; set; }
    public string TestProp { get; set; }
    System.Windows.Application App;

    public TestExternal(System.Windows.Application app)
    {
        this.App = app;
        UserProjects = new ObservableCollection<string>(new List<string>() { "a", "b", "c" });
        TestProp = "A";

        Task.Factory.StartNew(() =>
        {
            Thread.Sleep(5000);
            App.Dispatcher.Invoke((Action)delegate
            {
                TestProp = "C";
                UserProjects.Add("d");
            });
        });
    }
}

TestInternal.cs

// This class is in the WPF app project
public class TestInternal : BaseViewModel
{
    public ObservableCollection<string> UserProjects { get; set; }

    public string TestProp { get; set; }

    System.Windows.Application App;

    public TestInternal(System.Windows.Application app)
    {
        this.App = app;
        UserProjects = new ObservableCollection<string>(new List<string>() { "a", "b", "c" });
        TestProp = "A";

        Task.Factory.StartNew(() =>
        {
            Thread.Sleep(5000);
            App.Dispatcher.Invoke((Action)delegate
            {
                TestProp = "C";
                UserProjects.Add("d");
            });
        });
    }
}

XAML

<TextBlock Text="{Binding TestProp}" Style="{StaticResource NaviHeading}" />

In order for Fody to inject into the external assembly, you have to specify the appropriate weavers.

To indicate what weavers run and in what order a FodyWeavers.xml file is used at the project level. It needs to be added manually. It takes the following form:

<Weavers>
    <PropertyChanged />
</Weavers>

See https://github.com/Fody/PropertyChanged#add-to-fodyweaversxml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM