Struct in C# and ListView with Xamarin.Forms : how to match that?

antinetaantineta CAMember ✭✭
edited May 2015 in Xamarin.Forms

Hello,

I have a question about struct in C# and Xamarin.Forms.

I have created a struct for a specific kind of object because I don't have functions related to it : it's simply a group of variables. I try, in an XAML file, to get this struct directly and show in labels the different variables, but I don't know how this can be work. Have any idea?

Here's my XAML code :

<ListView VerticalOptions="FillAndExpand"
                      BackgroundColor="Transparent"
                      HasUnevenRows="true"
                      ItemsSource="{x:static local:App.benefitData}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <AbsoluteLayout VerticalOptions="FillAndExpand"
                                            HorizontalOptions="FillAndExpand">
                                <StackLayout AbsoluteLayout.LayoutFlags="All"
                                             AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
                                             Padding="10, 10, 10, 10">
                                    <Label Text="{Binding App.benefitData.Title}" />
                                </StackLayout>
                            </AbsoluteLayout>
                        </ViewCell>
                    </DataTemplate>
            </ListView.ItemTemplate>
</ListView>

And here's my struct :

public struct BenefitProtectionSection
{
     public string sectionCode;
     public List<string> detailsList;
}

public struct BenefitProtection
{
     public string benefitName;
     public string protectionType;
     public List<BenefitProtectionSection> sectionList;
}

public static List<BenefitProtection> benefitData;

Thanks in advance for any help, I really don't know how to get rid of that!

Tagged:

Posts

  • NMackayNMackay GBInsider, University mod

    @antineta

    Well Title isn't a property of BenefitData so that's not going to work.

    You'd be better setting the BindingContext of the view to your VM and binding the Listview to benefitData.

    In your Datatemplate try binding to benefitName, just to make sure the binding is working, your output window should show the binding error.

  • antinetaantineta CAMember ✭✭

    @NMackay,

    Thanks for your answer. I don't know how to bind benefitName because it is not a variable in a class. How can I bind it if it is in a struct?

    I'm a beginner with Xamarin, thanks for your help.

  • NMackayNMackay GBInsider, University mod

    @antineta

    Sorry, I should have looked closer.

    Why are you using struct? usually in MVVM apps you would uses classes that implement INotifyPropertyChanged to handle the binding between view and viewmodel. If you use an MVVM framework like MVVM Light that plumbing is built in if you inherit the base classes.

    Generally these properties your using would be properties of a view model used by your page/view binding context and would be IList/List or ObservableCollection.

    I can send you an MVVM sample if you want to get started, just send me a PM.

  • antinetaantineta CAMember ✭✭

    @NMackay,

    Thanks for your answer.

    So I supposed it's impossible without a class to do what I want to do?

  • NMackayNMackay GBInsider, University mod

    @antineta

    I'm not saying it's impossible but I'd use struct to expose static members for stuff like form layout etc

    For data binding uses classes and if you want to modify the data in the VM and automatically update the UI you need containers etc that implement INotifyPropertyChanged, something like ObservableCollection. You can bind to static members but for data binding it's not the way to go...imo.

  • antinetaantineta CAMember ✭✭

    @NMackay,

    Thanks for your answer. I will take your right way...and transform my struct into classes.

    Thanks for your help, and have a very nice day!

  • NMackayNMackay GBInsider, University mod
    edited May 2015

    @antineta

    No problem, as I say, just PM me if you want a little sample app.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    In case it wasn't clear yet why you can't use structs the answer is that structs are by-value objects in C#. That means every time you assign a struct object to a new variable (even in a function call argument) you are getting a new copy of the data. Therefore it doesn't really make sense to use something like binding, which relies on the idea of there being a specific shared instance with a state that you can synchronize with. Also, for similar reasons structs in C# can't implement interfaces so you could even implement the INotifyPropertyChanged interface if you wanted to.

    The only reason to use a struct in C# is if the object is small and immutable, and therefore copying it is both more efficient and safe. That doesn't apply here.

  • antinetaantineta CAMember ✭✭

    @adamkeep,

    Thank you for your notice. It will be helpful for me!

    Have a good day!

Sign In or Register to comment.