eYe
eYe

Reputation: 1733

WPF designer error due to incorrect running assembly path

I have a control that upon loading parses an XML file with country names and later populates a ComboBox. I access my XML as following:

 public DataTable GetData()
    {
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();

        ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + @"..\..\Resources\XML\countries.xml");
        return ds.Tables[0];
    }

GetData is called inside control's Loaded handler.

At design time, however, I get an error saying that directory does not exist with the XML file and as you see it is looking in the wrong AppDomain path (Visual Studio, not project):

enter image description here

How can I resolve this behaviour?

Upvotes: 2

Views: 197

Answers (1)

Bradley Uffner
Bradley Uffner

Reputation: 16991

The typical way to avoid this is to wrap the code in a check to see if it is running in the designer.

DesignerProperties.GetIsInDesignMode(this)

Will return true in design mode, and false when the app is running "for real" (if called from a DesignerObject). You should only try to read your xml when not in design mdoe.

Upvotes: 2

Related Questions