Forum Xamarin.iOS

Cannot bind data in ListView on iOS - works on Android

Neil3DsNeil3Ds Member ✭✭

Hello,

I'm trying to display bound details from an observable collection in a ListView. My ListView looks like this...

    <ListView x:Name="lvShifts" ItemSelected="lvShifts_ItemSelected" SeparatorVisibility="None" RowHeight="110" >
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>

                <StackLayout Margin="5,0" Orientation="Vertical"  BackgroundColor="LightGoldenrodYellow">
                  <!-- Divider -->
                  <StackLayout HeightRequest="3" BackgroundColor="Gold" Margin="0,0" ></StackLayout>
                  <StackLayout Margin="0.0" Orientation="Vertical">
                                            <StackLayout Orientation="Horizontal">
                      <Image WidthRequest="24" Source="{Binding Icon}" />
                      <Label FontSize="Small" FontAttributes="Bold" Text="{Binding DisplayDateTime}" />
                    </StackLayout>
                    <StackLayout Orientation="Horizontal" >
                      <Image WidthRequest="24"/>
                      <Label FontSize="Small" Text="{Binding DisplayLocation}" TextColor="Gray"/>
                    </StackLayout>
                  </StackLayout>
                  <StackLayout Orientation="Horizontal" >
                    <Image WidthRequest="24"/>
                    <Image WidthRequest="24" Source="MAInfo"/>
                    <Label FontSize="Small" Text="Tap for more info." VerticalTextAlignment="Center" />
                  </StackLayout>
                </StackLayout>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>

and Icon, DisplayDateTime and DisplayLocation are all properties (get/set) in the class.

The relevant code behind logic is this ...

ObservableCollection ocShifts = null;
if (objShifts != null)
{
ocShifts = new ObservableCollection(objShifts);
lvShifts.BeginRefresh();
lvShifts.ItemsSource = ocShifts;
lvShifts.EndRefresh();
}

This all works on Android and until upgrading to iOS 13.5, it worked on Apple devices too. But in every iOS simulator I've tried, nothing appears. If the text is hard coded (Text="Test") it works and the word "Test" appears where I'd expect it. Change this back to Text="{Binding DisplayDateTime}" and nothing is shown - not even a blank line. The only text displayed is the 3rd line, the info icon & "Tap for more info".

The collection itself is fine and the number of (primarily empty) rows is also correct - i.e. I have 2 results and I get 2 separate sets of entries saying "Tap for more info", but nothing is bound, not even blanks.

To me it seems like a bug in the iOS logic or the simulator - rather than my code, though in fairness I would say that! :-)

Has anyone else come across this. As I say prior to my XCode update, this same logic worked. So either I'm doing something wrong and it was luck before, or something's broken now.

Many thanks.

Tagged:

Best Answers

  • Neil3DsNeil3Ds Member ✭✭
    Accepted Answer

    Hi,

    OK, marvellous ... thanks to your help, I've narrowed it down to the linker settings - both in my project and in your sample. In particular...

    • Setting the link to "Link All", binding doesn't work
    • Setting it to "Link Framework SDKs" or "Don't Link", the binding does work
    • Between builds its important to save the project and clean the solution (I had somewhat mixed results if I didn't do that)

    One other "funny" - even if I have "Don't Link", when in iOS mode, VS2019 pops up an error band saying XAML Hot Reload won't work because I don't have "Don't Link" set up (even when I do).

    But yes, if I alter my linker settings to not "Link All", its all working as before.

    Thank you so much again for your help.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    I tested this with a simple data but it displays correctly on iOS 13.6:

    Here is my items source:

    ObservableCollection<MainModel> ocShifts = new ObservableCollection<MainModel>();
    for (int i=0; i<2; i++)
    {
        ocShifts.Add(new MainModel { DisplayDateTime = "date " + i, DisplayLocation = "location" + i });
    }
    lvShifts.ItemsSource = ocShifts;
    

    You could see data0, data1, location0, and location1 shown on the screen.
    Could you please share a sample here to help me reproduce this issue?
    You could also test my sample on your side to check the result.

  • Neil3DsNeil3Ds Member ✭✭

    Hi,

    Thanks very much for your response. I tried your example and got the same results here as I had before, which at least means my class logic is probably OK. I used the following code...

                ObservableCollection<Shift1> ocShifts = new ObservableCollection<Shift1>();
                for (int i = 0; i < 2; i++)
                {
                    ocShifts.Add(new Shift1 { DisplayDateTime = "date " + i, DisplayLocation = "location " + i });
                }
                lvShifts.ItemsSource = ocShifts;
    

    Shift1 is defined as ...

    {
        public String DisplayDateTime { get; set; }
        public String DisplayLocation { get; set; }
    }
    

    and on Android it displayed correctly...

    On the iOS simulator though, the same code displayed this...

    So, to the differences. I'm running on a Windows machine (Visual Studio 19) paired to a MacInCloud machine with the "latest" XCode. That said, the simulator options I can access are...

    ... and so I'm not running 13.6 which you're using, I only have access to 13.5.

    It does seem strange and almost one of those odd bugs that's caused by a certain combination of things I'm doing. Though like I say it used to work - and for you it appears it still does! Do you think it could be linked to 13.5 vs 13.6 ?

    I will continue to delve deeper. Its a nuisance though, normally its the AppStore bit that's painful, this time I haven't even got that far! :)

    Thanks again, Neil

  • LandLuLandLu Member, Xamarin Team Xamurai
    edited July 2020

    Do you think it could be linked to 13.5 vs 13.6?

    I'm not sure about this. You could try this with the latest iOS version or an older one.
    Try to update the XCode, VS for Mac, and Visual Studio.
    The latest version of XCode has come to 11.6:
    https://developer.apple.com/documentation/xcode-release-notes
    And we could test iOS 13.6 with it.

  • Neil3DsNeil3Ds Member ✭✭

    Thank you. I have updated VS2019 on Windows and am waiting for the Mac side of things to be updated. Once it is I will try the above tests again.

  • LandLuLandLu Member, Xamarin Team Xamurai
    edited July 2020

    I tested this with 13.5, still got the same result:

    I've no idea why binding doesn't work on your side. It appears to be an environmental issue.
    Have you changed the linker behavior or added a special mtouch argument?
    Did you test my sample directly on your simulators?

  • Neil3DsNeil3Ds Member ✭✭

    Hi,

    Well thanks to you we're making progress. I've updated VS2019 on Windows and the Mac to iOS 13.6 and the issue is exactly the same. Which is sort of good, because I wouldn't really want issues on specific releases. I also ran your sample and it works fine, again on 13.6 but its a good comparison. The same logic in my project fails as before.

    So its either the build configuration or linked to the machines I'm running against.

    Have I changed the linker? Possibly - the original project was written in a previous version of Visual Studio. It had been updated a few times but with VS2019 on new hardware, it seemed a better idea to start a new Xamarin forms project (with the latest forms, versions etc.) and copy across the xaml and .cs files etc. So it was a new project. I did check the configuration but some of the options were different between the VS releases and so they may well have changed. I will look at the build config. next to see if I can see anything there. The art of binding (or not binding) the data seems somewhat fundamental though, so its not terribly good if something there could simply break it?!

    I'll be honest I don't know what an "mtouch argument" is, so unless I've inadvertently done it, I haven't added anything like that. In fact this page and the underlying logic hasn't been touched at all since the previous working versions.

    The only other change - which was iOS specific was I had to create a new provisioning profile. I will compare the build settings now to see if that sheds any light on it.

    Thank you again for your help. It is much appreciated.

  • Neil3DsNeil3Ds Member ✭✭
    Accepted Answer

    Hi,

    OK, marvellous ... thanks to your help, I've narrowed it down to the linker settings - both in my project and in your sample. In particular...

    • Setting the link to "Link All", binding doesn't work
    • Setting it to "Link Framework SDKs" or "Don't Link", the binding does work
    • Between builds its important to save the project and clean the solution (I had somewhat mixed results if I didn't do that)

    One other "funny" - even if I have "Don't Link", when in iOS mode, VS2019 pops up an error band saying XAML Hot Reload won't work because I don't have "Don't Link" set up (even when I do).

    But yes, if I alter my linker settings to not "Link All", its all working as before.

    Thank you so much again for your help.

Sign In or Register to comment.