Binding a contentview dynamically to a list view

FXamainFXamain Member ✭✭

I have four contentview's and I have to bind those contentview's to the view cell of a list view in Xamarin forms. I have to load this contentview's dynamically based on the button click(four buttons are available in the listview) which also is within the listview item template, Could someone help me on this

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    To do it they way you describe:
    Raise a command, using the item from the listview as the CommandParameter
    This way you don't have the UI quite so tightly intertwined. And the command can probably be reused elsewhere.

    Though personally I'm not thrilled with the sound of what you're describing. I'd bet that regular binding to the SelectedItem of the ListView combined with DataTriggers would get what you want in a more MVVM way. But that's a huge guess based on virtual no meaning description of what you're actually trying to accomplish.

  • FXamainFXamain Member ✭✭

    could you please provide bit more info with a rough example

  • FXamainFXamain Member ✭✭

    Binding the Ui dynamicaly inside the list view using C# will be helpful

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Commands and DataTriggers are really well documented. If they are new concepts too you then I'd suggest a proper tutorial. Commands are a central concept to MVVM/modern software design. If they're new to you then you need to just stop and get a handle on them.

    http://redpillxamarin.com/2018/03/12/2018-101-vs2017-new-solution/

  • FXamainFXamain Member ✭✭
    edited March 26

    Below is the content view I have added
    <ContentView xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

             x:Name="demoscreen"
             x:Class="Views.Demo" >
    

    within the listview item template

    <Button HorizontalOptions="StartAndExpand" VerticalOptions="Center" BackgroundColor="#2b78e4" Image="listicon_white" Padding="5,0,5,0" Command="{Binding Source={x:Reference demoscreen}, Path= BindingContext.ActionTypeSelectedCommand}" CommandParameter="{x:Static local:PageType.FirstContent}"

    />
    on clicking the button I will be binding the contentview I have in the Contentview layout below.

    <ContentView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BindingContext="{x:Reference demoscreen}" Content="{Binding SelectedView}"

    />

    and in the C# section in MVVM, I will be binding the contentview to the the "SelectedView" property of the View Model as below

    SelectedView = new Views.FirstContent(parameter1, parameter2);

    But it is not binding the content to the UI.

    Am I doing anything wrong

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    1 - No idea why you posted that little bit of XAML
    2 - You need to use markdown in your posts here to keep the site from trying to render your XAML.
    3 - Even in that small amount of XAML I see something that leads me to have concerns. Why are you assigning an x:name? That's almost always a sure sign of someone trying to access the XAML of C# code behind like its the year 2001.

    I linked a tutorial site for you. Stop coding and work the tutorials from end to end. It shouldn't take you more than a few hours and will SAVE you time and frustration in the long run.

    Clearly you've launched Visual Studio then just jumped in thinking "Oh, I'll figure it out as I go along". That's not a good approach.

    Start with working the free textbook from cover to cover.
    https://blogs.msdn.microsoft.com/microsoft_press/2016/03/31/free-ebook-creating-mobile-apps-with-xamarin-forms/

    Feel free to hit up on-line tutorials sites. There are plenty. Here's mine:

    When you say

    Its my first time using Xamarin.

    If you really mean

    Its my first time doing any coding at all

    Then stop and don't get into Xamarin at all yet. First you need to learn C# which is the language all your logic and actual 'code' will be written in. Xamarin is an eco-system that your code operates within so that the same code can run on iOS, Android, UWP, MacOS and Tizen without having to create separate solutions for each platform. But if you can't first write code - any code - then it does you no good to try to figure out cross-platform deployment.

    Once you are at least somewhat comfortable in C# then look into MVVM design. It is the foundation concept that any app in the last 15 years should be based on, whether that's a Windows WPF program or a Xamarin cross-platform app. If you have no experience with MVVM design... no experience with databinding code to UI... etc. etc. then you need to first get a handle on that.

    Then realize you can't just jump into make your "goal application" as your first project. Your first application can't be an entire inventory and ordering system for your uncle's restaurant, or scheduling app for your sisters delivery service.

    You have to start with building a bird house... then a dog house... then an outhouse... shed... garage... Before you try to build a 3 story apartment building. You have to apply that same "start small and learn" approach to software development. You start by building small apps alike alarm clocks and mortgage calculators, and work up from there learning UI development, OOP and other basics along the way. Then you take those skills to your next bigger project. In other words you have to learn how to be a coder before you can be a software architect.

    When you're ready to start designing an intermediately complex app just tackle one issue at a time in a "Sandbox" or R&D app.
    Start with the first basic need: People
    That's not Xamarin in any way. Just define a Person class. And then inherit from that for a User class. And you'll want a Rights class to be the permissions for that User object.

    Then probably some type of grouping... whether its groups of people or groups of things. Learn how to work with collections of objects.

    But learn each of your bullet points one at a time in a little R&D app before you try to take on putting them all together. You need to understand them as a single thing before you can architect how they will interact.

    In the end... 90% of of the typical app has nothing to do with Xamarin. Its all basic-to-intermediate C#, application architecture and design that would be the same in Xamarin, WinForms, WPF, Android, ...

Sign In or Register to comment.