Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am trying to do some design in Blend 4. The sample data function of Blend is nice to have during design time, but it could be much more better to see the design in browser.

Can anyone help me to use sample data created by Blend as real data (see same data in browser) also ?

Thanks in advance.

share|improve this question

2 Answers 2

Hopefully your application is using MVVM pattern so that it can manufacture Blend data. I believe this is the recommended approach. If you are unfamiliar with it, definately take some time to get familiar with it.

I've inlcuded some code snippets below, but don't fret about the code too much, just follow the pattern.

To blend your data:

Set up MMVM in your project and in the View Model constructor, check to see if you are in Blend. Pass in an agent that will handle getting the data for you.

public MainPageViewModel(IUserAgent userAgent)
        {
            //Design Mode Check, use WCF when not in design mode
            if (!base.InDesignerTool)
            {
                if (userAgent != null)
                {
                    UserAgent = userAgent;
                }
            }
            else // In Cider (CS 2010 designer) or Expression Blend, don't call WCF, so use the UserAgentDesigner instead
            {
                UserAgent = new UserAgentDesigner();
            }

            //Wire up the commands to the view model.
            this.WireUpCommands();
        }

We have an interface to populate the view, called IUserAgent, and two implementations, one for a service call and then one for the design time (fake data):

namespace SilverlightApplicationChannelWCF.ServiceAgent
{
    /// <summary>
    /// The User Agent Interface
    /// 
    /// We use an interface so the the get data method is loosely coupled with the view model.
    /// 
    /// We could get the data from a WCF service, or manufacture data if in a design environment like
    /// Expression Blend.
    /// </summary>
    public interface IUserAgent
    {
        /// <summary>
        /// Gets users
        /// </summary>
        /// <param name="callback">The call back when the call completes.</param>
        void GetUsers(System.EventHandler<DataTransferObjectEventArgs> callback);

        /// <summary>
        /// Saves users
        /// </summary>
        /// <param name="users">The users to save.</param>
        /// <param name="callback">The call back when the method completes.</param>
        void SaveUsers(DataTransferObject dto, System.EventHandler<DataTransferObjectEventArgs> callback);
    }
}

Then we have two implementation classes, one that gets data from the service:

public class UserAgent : IUserAgent  

And another that manufacturers data for the designer:

 public class UserAgentDesigner : IUserAgent

Each implementation returns back the same type of DTOs, for designer the data is manufactured:

        /// <summary>
        /// Gets users.
        /// </summary>
        /// <param name="callback"></param>
        public void GetUsers(System.EventHandler<DataTransferObjectEventArgs> callback)
        {
            //Wire up the call back
            this.AgentGetUsersCompleted += callback;

            DataTransferObject dto = new DataTransferObject();
            //Return the users
            List<BaseDTO> users = new List<BaseDTO>();

            users.Add(new UserDTO() { FirstName = "Jon", LastName = "Raynor (Design Data)", IsDirty = false });
            users.Add(new UserDTO() { FirstName = "Sunil", LastName = "Munikar(Design Data)", IsDirty = false });
            users.Add(new UserDTO() { FirstName = "Aaron", LastName = "McIver(Design Data)", IsDirty = false });

            //Assign users to DTO
            dto.DataTransferObjects = users;

            //Setup and assign event args
            DataTransferObjectEventArgs e = new DataTransferObjectEventArgs();
            e.Dto = dto;

            //Raise an event for the callback
            if (!ReferenceEquals(null, AgentGetUsersCompleted))
            {
                AgentGetUsersCompleted(this, e);
            }
        }

Now, you can hand off your applicate to Blend and it will populate with data without using a WCF service call so that UI can be designed and rendered with data.

share|improve this answer
    
Thanks for the detail answer. your explanation of the service-agent to help distinguish the design/real time data is very understandable. But what while i was looking for blend generated "sample data"(which is in XAML format) to be used in GetUsers() method of UserAgentDesigner. In short my idea is to initialize the UserDTO object from XAML class that Blend generates while using the sample data feature. –  Sunil Munikar Jan 27 '12 at 13:27
    
@Sunil - I am not sure if Blend can do that, it would be a nice feature/API call if it exists. –  Jon Raynor Jan 27 '12 at 14:25

Design time data and run time data are different.

You can certainly use the same class structure you created for your sample data when setting up your Create Object Data Source backing which could provide you with your need of leveraging the structure you defined for the sample data.

share|improve this answer
    
there is different run time data, which is not ready yet. and as you mentioned i am using the same class structure to create my sample data. was just wondering if there is some option to use those generated sampledata to see them in browser as well. –  Sunil Munikar Oct 28 '11 at 8:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.