Error Unable to create an instance of "ObjectName" in Designer when using <UserControl.Resources>
I am trying to associate the source of a combobox element with a static resource. I am doing this to understand what I am doing.
So I created a class
public class A : ObservableCollection<string> { public A() { IKBDomainContext Context = new IKBDomainContext(); Context.Load(Context.GetIBOptionsQuery("2C6C1Q"), p => { foreach (var item in SkinContext.IKBOptions) { this.Add(item); } }, null); } } Thus, the class has a constructor that populates itself using a domaincontext, which receives data from a persistent database. I only read on this list, so donβt worry about persisting.
In xaml, I add a reference to the namespace of this class, then add it as usercontrol.resources to the page control.
<UserControl.Resources> <This:A x:Key="A"/> </UserControl.Resources> and then I use it in this staticresource to bind it to my link list items. In fact, I have to use a data template to display this object correctly, but I will not add it here.
<Combobox ItemsSource="{StaticResource A}"/> Now that I'm in the designer, I get an error message:
Unable to create instance "A".
If I compile and run the code, it works fine. This only affects the editing of the xaml page.
What am I doing wrong?
When run in the designer, the full functionality of the application is not available. However, the designer does not just magically know how to mock the UserControl user interface. Its Xaml is parsed, and the objects described there are created.
It is up to you to code your classes to cope with the existence of a designer. You can use the static proeprty DesignerProperties.IsInDesignTool to determine if your code is used in the designer tool or not.
If in the designer you can provide a set of test data, rather than trying to access the data service.