Tech Off Thread

11 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

XAML Abuse - Dynamic XAML generation?

Back to Forum: Tech Off
  • User profile image
    ManipUni

    I'm just starting to get my feet wet with XAML/WPF. What I want to get a feel for is if the code drives the XAML or if the XAML drives the code. So let's say I load a file, and I want to generate UI elements based on that file - is that something XAML can handle?

    Just to be clear I'm not talking about databinding or similar. I am talking about literally being able to generate a new Text Box at runtime. This is something VB6 and C#/forms could handle with extreme reluctance (VC++ could handle VERY easily). Is XAML better suited to this kind of thing?

    edit:
    Just to be clear what I'm getting at - I want to know if, in theory, I could push all of my XAML out to an external file and pull that in on launch and that would be the UI of the application? Or if the user could select a theme from several and it would build the UI without a restart?

  • User profile image
    PerfectPhase

    You can do both.  You can add controls at runtime from code, you can load xaml from a database and bind against it, you can generate controls at runtime, save the resultant UI as a xaml string and then reload that.

  • User profile image
    ManipUni

    @PerfectPhase: Thanks. You don't happen to know of an example/tutorial/article/blog post on that? I searched for "dynamic XAML" and other similar terms and aren't getting much...

  • User profile image
    spivonious

    @ManipUni: Do you want to just change colors and fonts and such, or the entire interface? You can do the former very easily with styles and binding.

    As far as your XAML driving the code or vice a versa, the XAML gets compiled into code on build, so it's all code at run time.

  • User profile image
    contextfree

    @ManipUni: if you're willing to get really into WPF and generating controls in code, you could check out Charles Petzold's WPF book, he doesn't even introduce XAML at all until halfway through

  • User profile image
    PerfectPhase
  • User profile image
    Dr Herbie

    XAML is equivalent to a .resx file; it's a DSL for the UI processed at compile time.

    To dynamically set the UI at runtime you will have to build the object graph for the UI in code just as you would for a WinForms app. Although they are different libraries, they still come down to objects representing UI controls which are kept in an ownership graph.

    Unfortunately, the .NET framework does not seem to include a WPF design surface like it contains a Winforms Design surface, otherwise you could (relatively) easily write self-editing WPF applications.

    Herbie

  • User profile image
    AndyC

    I've done precisely that in an Excel app before, which had a WPF front-end for large screen presentations. The thing you need to be googling is XamlReader. There are a few limitations with what you can do with that, but it's the easiest way. Of course you can also always create controls at runtime and use them to populate a form as required if you need something more complex.

    As mentioned above, the Petzold book Code+Markup is definitely worth checking out if you want to get a feel for how to do this manually.

     

  • User profile image
    ManipUni

    Thanks everyone for your help. I will be looking at XamlReader and the MSDN/GeeksWithBlogs links. I don't have much time right now so I will look at this in much more detail on the weekend.

  • User profile image
    PerfectPhase

    , Dr Herbie wrote

    XAML is equivalent to a .resx file; it's a DSL for the UI processed at compile time.

    Actually I'd disagee with that a bit  Smiley  XAML is a stright OMV based object seralisation system, doesn't care what you use it for (see XamlServices.Save()) the MS-WPF grammar is the DSL for WPF.

  • User profile image
    Dr Herbie

    @PerfectPhase: I wasn't thinking in technical terms, I was thinking in it's place in the development process.  I guess 'DSL' was the wrong phrase to use.

    Herbie

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.