Forum Xamarin.Forms

Passing data in xaml

AdrienAdrien Member ✭✭

hi everyone,

I've got trouble to pass data in xaml. I would like to initialize a component named PDebugListPage passing a list of business items :

ListeLogs is a List declared in the ViewModel. The constructor of PDebugListPage is :

I don't know how to "bind" the ListeLogs between my xaml and my viewmodel.

Thanks for your answer !

Adrien.

Best Answer

Answers

  • AdrienAdrien Member ✭✭

    Thank you for your answer, i already read this but i still don't understand how to pass my variable already filled ListeLogs to my PDebugListPage.

    Maybe my problem is not well explained.

    Projet 1 :
    DebugListPageTestViewModel.cs => Initialize a bindable variable ListeLogs (List of PLogRecupItem)
    DebugListPageTest.xaml => Try to pass in xaml this variable ListeLogs to the constructor of DebugListePage of the project 2

    Project 2 :
    DebugListPage.xaml.cs => Have a constructor with a list of PDebugListItem parameter

    I have a workaround which consist to add my DebugListPage directly in C# but it is not what i want to realize :

  • ansotoansoto Member ✭✭

    @Adrien

    Viewmodel to View data passing is all done through Bindings.

    Similar to the link posted above, you can set the Binding context of DebugListPage to an instance of DebugListPageViewModel and use a binding in your DebugListPage.xaml to retrieve this list.

    DebugListPage.xaml.cs:
    public DebugListPage()
    {
    this.BindingContext = new DebugListPageViewModel();
    InitializeComponent();
    }

    DebugListPage.xaml
    ... = {Binding ListeLogs}

    Is there a specific reason you are trying to pass it from the DebugListPageTest.xaml? You can have your DebugListPage bind directly to the ViewModel property.

    References:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/basic-bindings
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/data-and-databinding

  • ColeXColeX Member, Xamarin Team Xamurai
    edited February 13

    I suggest the way as ansoto said , set the model as BindingContext in Page Constructor.

    You could also set BindingContext in Xaml

    <ContentPage.BindingContext>
        <local:Model/>
    </ContentPage.BindingContext>
    

    Model

    public class Model
    {
    
        public List<Item>List
        {
            get;set;
        }
    
    
        public Model()
        {
            List = new List<Item> { };
        }
    }
    

    Usage

    <ContentPage.Content>
        <ListView ItemsSource="{Binding List}"/>
    </ContentPage.Content>
    
  • AdrienAdrien Member ✭✭

    @ColeX @ansoto Thank you for your time and your answers.

    @ansoto said:

    Is there a specific reason you are trying to pass it from the DebugListPageTest.xaml? You can have your DebugListPage bind directly to the ViewModel property.

    Why in xaml instead of c# ? It looks like more readable to me. UI in xaml and logic in C#, as possible.

    The problem is, I want two separates viewmodels (maybe my architecture is wrong) :

    • DebugListPageTestViewModel : Create The ListeLogs and various UI binding variables for DebugListPageTest
    • DebugListPageTest : Create the UI, with a component DegugListPage and other UI Component

    • DebugListPageViewModel (or DebugListComponentViewModel would be better) : Get the ListeLogs and Create a ListeLogRefactor base on ListeLogs, make some filter, sort, ...

    • DebugListPage (or DebugListComponent would be better) : Display the component (CollectionView, itemsource binding = ListeLogRefactor

    And tomorrow :

    • SomePageViewModel: Create an other ListeLogs
    • SomeListPage : Create the UI, with component DegugListPage

    Then if i want to change the way i sort data, i just have to change DebugListPage instead of SomeListPage and DebugListPage

    Adrien.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    @Adrien said:

    @ansoto said:

    Is there a specific reason you are trying to pass it from the DebugListPageTest.xaml? You can have your DebugListPage bind directly to the ViewModel property.

    Why in xaml instead of c# ? It looks like more readable to me. UI in xaml and logic in C#, as possible.

    Last time I checked, List<T> was not a UI component. Sounds like logic to me.

    Can you share PDebugListPage.xaml and PDebugListViewModel.cs? Knowing what you're trying to do with the data you want to pass as a constructor argument might help us to find a better way for you.

  • AdrienAdrien Member ✭✭

    @JoeManke

    You can find a sample with two projects :

    • BusinessApp
    • ComponentLibrary

    In the MainPage of the BusinessApp, i have an instance of my DebugListComponent

    MainPage.xaml.cs :

    MainPage.xaml :

    If you see some bad code, feel free to tell me.

Sign In or Register to comment.