Forum Cross Platform with Xamarin

Xamarin Forms Views proper use.

In plain old Windows Forms I used to place views on the form and then fill them with UserControls. I could then swap out the UserControls for others base on what action the user did while running the program (Button push, etc).
I want to do the same thing in Xamarin Forms. I cannot find an example using views. Perhaps, views in Xamarin don’t mean the same as they did in Windows Forms.



  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited October 2018

    I want to do the same thing in Xamarin Forms.

    No. You really don't. Nobody in 2018 would tolerate that 1998 way of doing things, even in a new WPF app - and by no means would any software house accept that for a modern cross-platform app in today's world.

    Perhaps, views in Xamarin don’t mean the same as they did in Windows Forms.

    ContentPage is a page
    ContentView is a view you would put on a Page
    View is the superclass that both of these would derive from.
    More importantly: You shouldn't be guessing if it does or doesn't or what any of the things are based on their names.

    Take it from someone that did WPF professionally for 15 years and thought: "Oh, this is just another XAML/MVVM. I can pick this up in a month." OH HECK NO. Don't just jump in and try to reverse-engineer an understanding of this based on your experience with WPF. Take the time. Do the Xamarin University course. Do the tutorials out there.

    • Best case: Its all simple and takes you no time at all. No harm, no foul.
    • Likely case: You'll pick up a lot of things you didn't realize that are different. Meaning 100 hours will save you 1,000 hours of hair pulling.
    • Worst case: The courses and training take you a significant amount of time, meaning you really needed them afterall.
  • JohnHardmanJohnHardman GBUniversity admin

    @rgordey - If I've understood correctly what you are trying to do, you can use a ContentView and replace its Content, or use one of the Layout types (e.g. StackLayout, AbsoluteLayout, RelativeLayout, Grid etc) and replace their Children. I make use of both techniques.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Ok. Let me ask this then if you're doing that... How the heck are you maintaining separation of UI and logic and any sense of MVVM? To replace all the UI, you have to generate it all presumably from UI code behind because, well, its UI - so that's not taking place in a VM. But that means you're code behind has to know the properties of the VM to bind to... giving you a tightly coupled situation with the VM. Seems like a nightmare

  • JohnHardmanJohnHardman GBUniversity admin
    edited October 2018

    @ClintStLaurent - I use these techniques in a variety of scenarios. A few examples:

    (1) Because MasterDetailPage was so buggy for such a long time (perhaps it still is - I don't know), and because it didn't do quite what I wanted, I wrote my own layer on top of XF that allows me to display more than one "page" at a time. I'll be simplifying here - effectively, I have one top level ContentPage containing a horizontal StackLayout, each child of which is a ContentView. The functional pages are built as they normally would be, but based on my own subclass of ContentPage. The layer handling multi-page mode pulls the Content from those subclasses and sets the ContentView.Content of the appropriate StackLayout child with that Content. Voila - the appearance of multiple functional pages at the same time. The functional pages use MVVM as you would expect. The multi-page mode is a bit of magic on top.

    (2) Expandable Views. Again simplifying - a ContentView containing a vertical StackLayout containing a horizontal StackLayout, one child of which is an expander Button. Press the expander Button and the Children of the vertical StackLayout are replaced to display a contractor Button and the expanded detail of whatever the VM contains. Again, using appropriate subclasses, MVVM is maintained between the detail panel and the VM.

    (3) An Add/Edit page, the content of which is built using templates, the templates being chosen on context. Each view template actually maps onto the same VM, but displaying the content differently.

    I suspect there are more scenarios, and possibly cases where I haven't maintained MVVM, but if so, I'll probably clean those up during re-factoring :-)

    Of course, the OP's subject/title referred to "proper use". Whether what I do is "proper", well... ;-)

Sign In or Register to comment.