开发者

WPF xaml: Is it possible to load data differently for VS preview than runtime?

In the Visual Studio preview window, I can populate my window with data using XmlDataProvider:

<XmlDataProvider x:Key="Foo" Source="Foo.xml" XPath="document"/>

However, if at runtime this file cannot be found th开发者_开发问答en the user just gets a crash message like "The program has stopped working". How can I change this so that I have more control over how the data is loaded at runtime (e.g. specify a source folder, handle any exception that occurs, show an error message)?


There are multiple approaches to providing design time data with regard to WPF. Sacha Barber provides one approach; complete with a demo project near the bottom.

Josh Smith also provides his approach which is similar and yet another simple approach to providing design time data.

Lastly is that VS2010 provides the ability to set design time data within the IDE as does Expression Blend if you are making use of that product.


Use binding instead. Try a ViewModel (MVVM). You can set the source manually at design time to get the visual so you can design it but once you're done, remove it and use binding. At run time, determine what data to load and WPF will show it if it's there or not if it isnt. You can control the errors that might occur if the xml is not present.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜