Search code examples
xamarinwindows-store-appsmvvmcross

Activator with MvvmCross and PCL


I have a very strange problem. Currently, I am porting a Windows Universal App (Win 8.1 + WinPhone 8.1) with Prism to a CrossPlattform Solution with MvvmCross. First, a big thanks for all the work with MvvmCross - it is amazing.

My problem is now with the System.Activator class. I used this approach for my validation in my Universal App and I tried to port it. Now everything is compiling fine but at runtime an nullreference exception gets thrown. I figured out that it is the Acitivator that is null. When I try to access it in the Immediate windows it says:

error CS0103: The name 'Activiator' does not exist in the current context

The code is executed in every Model and implemented in a base class constructor:

protected ModelBase()
{
    foreach (var property in this.GetType().GetRuntimeProperties())
    {
        var type = typeof(Property<>).MakeGenericType(property.PropertyType);
        var prop = (IProperty)Activator.CreateInstance(type);
        this.Properties.Add(property.Name, prop);
        prop.ValueChanged += (s, e) =>
        {
            RaisePropertyChanged(property.Name);
            Validate();
        };
    }
}

Any ideas?


Solution

  • Thanks for all the answers. I am feeling a little embarrassed now, because I figured the problem out and it was totally my fault :).

    @Anders: You was right, I misspelled the Activator in the Immediate windows and that is why the error was there in the first place - stupid.

    The second Problem was the ctor of the generated Object.

    public Property()
    {
        this.Errors.CollectionChanged += (s, e) => RaisePropertyChanged("IsValid");
        Errors = new ObservableCollection<string>();
    }
    

    Did you find the error? Jap, hooking up to an event on a not created object is a bad idea. Sorry for wasting your time :/ and thanks.