GrayFox
GrayFox

Reputation: 1089

Generic class as base causes XAML error

I have the classes customer and product. Their similarities are in the class Base Record. However, the types in Base Record always depend on whether it is a customer or product. Therefore, I have Base Record created generically.

As soon as customer or product inherits from Base Record , I get XMAL-error in my WPF project. Unfortunately, I do not know why this is, why I write here ;)

Header of BaseRecord:

public abstract class BaseRecord<T> : Notify where T : class, new()

Header of Customer:

public class Customer : BaseRecord<Data_Layer.Customer>

XAML error:

   Unknown build error, 'Cannot resolve dependency to assembly 'Version=1.0.0.0,  Culture=neutral, PublicKeyToken=null' 
   because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on 
   demand through the ReflectionOnlyAssemblyResolve event.'

AND:

The tag 'CustomerAddWindow' does not exist in XML namespace 'clr-namespace:Business_Layer.Windows.Customer;assembly=Business_Layer'

If I make the class BaseRecord not generic everything works as I want.

in XAML, I instantiate

<local:Customer x:Key="CustomerViewModel "/>

Happy new year.

Upvotes: 2

Views: 349

Answers (2)

GrayFox
GrayFox

Reputation: 1089

I have found the error and want to show my solution for it:

I have added the reference to my WPF-Project where my classes are which I use in the generic one.

My architecture is:

Business_Layer for business logic

Data_Layer to maintain my data from DB

Presentation_Layer as View.

I didn't reference the Data_Layer to the Presentation_Layer because there was no need. But when you are using a generic class inside WPF-project you have to that WPF can resolve it.

That was the first error want to tell:

 Unknown build error, 'Cannot resolve dependency to assembly 'Version=1.0.0.0,  Culture=neutral, PublicKeyToken=null'because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.'

I don't know why but after adding this data_layer reference to presentation_layer, I don't need the workaround class. I can use

public class CustomerBase : BaseRecord<Data_Layer.Customer>

directly without any errors.

Upvotes: 0

Luis Filipe
Luis Filipe

Reputation: 8708

XAML does not support generics directly.

There are some side projects that aim in solving this issue but i always chose to live with it. It really never hindered my application architecture.

A workaround which i believe it's simple and effective is to have a mid-inheritance class which solves the generic.

For instance,

public abstract class BaseRecord<T> : Notify where T : class, new()

public abstract class CustomerBase : BaseRecord<Data_Layer.Customer>

public class Customer : CustomerBase

I currently develop a WPF application and i do not need any generics in my XAMLs.

I have zero code in the xaml code-behind. Truly, my base window class has code behind all UI-related due to some customizations.

If you follow the MVVM architecture that generic, which is likely to be a property in your base window should, most likely, belong in the ViewModel class.

Upvotes: 1

Related Questions