All views inside CarouselView are null

MassimilianoBruniMassimilianoBruni USMember ✭✭
edited December 2017 in Xamarin.Forms

I have defined a CarouselView in XAML as follows:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:controls="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions"
    x:Class="Youni.PresentationPage">
    <controls:CarouselViewControl
        x:Name="Carousel"
        Orientation="Vertical"
        IsSwipingEnabled="false"
        VerticalOptions="FillAndExpand"
        HorizontalOptions="FillAndExpand">
        <controls:CarouselViewControl.ItemsSource>
            <x:Array
                Type="{x:Type View}">
                <ScrollView>
                    <AbsoluteLayout
                        Padding="0"
                        Margin="0">
                        <Image
                            Source="login_icon.png"
                            AbsoluteLayout.LayoutBounds=".5, .0, .3, .3"
                            AbsoluteLayout.LayoutFlags="All" />
                        <StackLayout
                            Orientation="Vertical"
                            AbsoluteLayout.LayoutBounds=".5, .5, .9, .35"
                            AbsoluteLayout.LayoutFlags="All">
                            <Label
                                Text="Unisciti a Youni!"
                                FontSize="Large"
                                FontAttributes="Bold"
                                XAlign="Center" />
                            <Entry
                                x:Name="NameEntry"
                                VerticalOptions="FillAndExpand"
                                Placeholder="Nome" />
                            <Entry
                                x:Name="SurnameEntry"
                                VerticalOptions="FillAndExpand"
                                Placeholder="Cognome" />
                            <Entry
                                x:Name="NewEmailEntry"
                                VerticalOptions="FillAndExpand"
                                Placeholder="Email"
                                Keyboard="Email" />
                            <Entry
                                x:Name="NewPasswordEntry"
                                VerticalOptions="FillAndExpand"
                                Placeholder="Password"
                                IsPassword="true" />
                        </StackLayout>
                        <Button
                            x:Name="RegisterButton"
                            Clicked="Register_Handle_Clicked"
                            AbsoluteLayout.LayoutBounds=".5, .85, .3, .07"
                            AbsoluteLayout.LayoutFlags="All"
                            Text="Registrati"
                            TextColor="White"
                            BackgroundColor="#3A8FDA" />
                        <StackLayout
                            Orientation="Horizontal"
                            HorizontalOptions="Center"
                            AbsoluteLayout.LayoutBounds=".5, 1, 1, .1"
                            AbsoluteLayout.LayoutFlags="All">
                            <StackLayout.Spacing>
                                <OnPlatform
                                    x:TypeArguments="x:Double">
                                    <On
                                        Platform="Android"
                                        Value="-10" />
                                    <On
                                        Platform="iOS"
                                        Value="3" />
                                </OnPlatform>
                            </StackLayout.Spacing>
                            <Label
                                Text="Sei già registrato?"
                                FontSize="Small"
                                YAlign="Center"
                                XAlign="Center" />
                            <Button
                                x:Name="LoginSwitchButton"
                                Pressed="LoginSwitch_Handle_Pressed"
                                Released="LoginSwitch_Handle_Released"
                                Clicked="LoginSwitch_Handle_Clicked"
                                Text="Passa al login"
                                FontSize="Small"
                                TextColor="#45BFEE"
                                BackgroundColor="Transparent" />
                        </StackLayout>
                    </AbsoluteLayout>
                </ScrollView>
                <ScrollView>
                    <AbsoluteLayout
                        Padding="0"
                        Margin="0">
                        <Image
                            Source="login_icon.png"
                            AbsoluteLayout.LayoutBounds=".5, .215, .3, .3"
                            AbsoluteLayout.LayoutFlags="All" />
                        <StackLayout
                            Orientation="Vertical"
                            AbsoluteLayout.LayoutBounds=".5, .6, .9, .14"
                            AbsoluteLayout.LayoutFlags="All">
                            <Entry
                                x:Name="EmailEntry"
                                VerticalOptions="FillAndExpand"
                                Placeholder="Email"
                                Keyboard="Email" />
                            <Entry
                                x:Name="PasswordEntry"
                                VerticalOptions="FillAndExpand"
                                Placeholder="Password"
                                IsPassword="true" />
                        </StackLayout>
                        <Button
                            x:Name="LoginButton"
                            Clicked="Login_Handle_Clicked"
                            AbsoluteLayout.LayoutBounds=".5, .85, .3, .07"
                            AbsoluteLayout.LayoutFlags="All"
                            Text="Login"
                            TextColor="White"
                            BackgroundColor="#3A8FDA" />
                        <StackLayout
                            Orientation="Horizontal"
                            HorizontalOptions="Center"
                            AbsoluteLayout.LayoutBounds=".5, 1, 1, .1"
                            AbsoluteLayout.LayoutFlags="All">
                            <StackLayout.Spacing>
                                <OnPlatform
                                    x:TypeArguments="x:Double">
                                    <On
                                        Platform="Android"
                                        Value="-10" />
                                    <On
                                        Platform="iOS"
                                        Value="3" />
                                </OnPlatform>
                            </StackLayout.Spacing>
                            <Label
                                Text="Ancora senza account?"
                                FontSize="Small"
                                YAlign="Center"
                                XAlign="Center" />
                            <Button
                                x:Name="RegistrationSwitchButton"
                                Pressed="RegistrationSwitch_Handle_Pressed"
                                Released="RegistrationSwitch_Handle_Released"
                                Clicked="RegistrationSwitch_Handle_Clicked"
                                Text="Registrati"
                                FontSize="Small"
                                TextColor="#45BFEE"
                                BackgroundColor="Transparent" />
                        </StackLayout>
                    </AbsoluteLayout>
                </ScrollView>
            </x:Array>
        </controls:CarouselViewControl.ItemsSource>
    </controls:CarouselViewControl>
</ContentPage>

In the code-behind, I can handle the buttons Clicked events, but I can't access any of the views (entries, buttons, images etc...). Always throws System.NullReferenceException. Seems like all the views are null, which does not make sense.

Stack trace:

System.NullReferenceException: Object reference not set to an instance of an object
at Youni.PresentationPage+d__5.MoveNext () [0x00034] in /Users/Max/Documents/Xamarin/Youni/Youni/Youni/PresentationPage.xaml.cs:51
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018
at UIKit.UIKitSynchronizationContext+c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/UIKit/UIKitSynchronizationContext.cs:24
at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/Foundation/NSAction.cs:163
at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/UIKit/UIApplication.cs:79
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/UIKit/UIApplication.cs:63
at Youni.iOS.Application.Main (System.String[] args) [0x00001] in /Users/Max/Documents/Xamarin/Youni/Youni/iOS/Main.cs:17

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Please edit your post so the XAML isn't mangled by the site parser.
    Here is a how-to article
    https://redpillxamarin.com/2016/12/13/faq-frequently-asked-questions/

    All views inside CarouselView are null

    Sounds like your BindingContext isn't evaluating to what you think it is, at run-time.

  • MassimilianoBruniMassimilianoBruni USMember ✭✭
    edited December 2017

    @ClintStLaurent said:
    Please edit your post so the XAML isn't mangled by the site parser.
    Here is a how-to article
    https://redpillxamarin.com/2016/12/13/faq-frequently-asked-questions/

    OK now it shows fine.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Type="{x:Type View}"

    Are you sure about that? Have you tried ContentView ?

    YAlign="Center"

    That hasn't be a legal tag in years. Its from XF version 1.3 and was deprecated years ago. What are you using as your learning source materials?

    Why does everything have a name and no binding? That looks like you're not doing good XAML/MVVM and instead trying to do all the handling in C# code behind.
    Buttons are handling click events? Yeah, you're doing logic in the UI layer, huh?

    In the code-behind, I can handle the buttons Clicked events,

    Oh, I guess you said that. Unless it is specific to UI needs, if you're logic code in the UI code behind its bad practice.

    Lots of repetition instead of defining a style.

    but I can't access any of the views

    Bigger questions is why are you trying to? I'd urge you to clean this up and follow established MVVM patterns. This kind of mess is already causing you issues and it will only go downhill from here.

    Make each view as its own XAML.
    Add them to a bindable collection
    Set the collection as a property in your ViewModel
    Set that property as the source for the CarouselView.

  • @ClintStLaurent said:

    Type="{x:Type View}"

    Are you sure about that? Have you tried ContentView ?

    ContentView doesn't work.

    YAlign="Center"

    That hasn't be a legal tag in years. Its from XF version 1.3 and was deprecated years ago. What are you using as your learning source materials?

    OK I will use VerticalTextAlignment. I'm using this guide: https://developer.xamarin.com/guides/xamarin-forms/xaml/
    But I found that tag with auto-completion, I didn't find it online.

    Why does everything have a name and no binding? That looks like you're not doing good XAML/MVVM and instead trying to do all the handling in C# code behind.
    Buttons are handling click events? Yeah, you're doing logic in the UI layer, huh?

    In the code-behind, I can handle the buttons Clicked events,

    Oh, I guess you said that. Unless it is specific to UI needs, if you're logic code in the UI code behind its bad practice.

    Lots of repetition instead of defining a style.

    but I can't access any of the views

    Bigger questions is why are you trying to? I'd urge you to clean this up and follow established MVVM patterns. This kind of mess is already causing you issues and it will only go downhill from here.

    Make each view as its own XAML.
    Add them to a bindable collection
    Set the collection as a property in your ViewModel
    Set that property as the source for the CarouselView.

    I will follow your advices and I will come back here when I'll have a better model. Thanks.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Are you sure you're up-to-date? What version of Visual Studio, Xamarin plugin and Xamarin.Forms?

  • @ClintStLaurent said:
    Are you sure you're up-to-date? What version of Visual Studio, Xamarin plugin and Xamarin.Forms?

    Visual Studio Community for Mac 7.2.2
    Xamarin.Forms 2.5.0.91635
    CarouselView.FormsPlugin 5.0.2

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @MassimilianoBruni said:
    But I found that tag with auto-completion,
    Visual Studio Community for Mac 7.2.2

    @DavidOrtinau Are you / Xamarin dev team aware of this?
    He's not kidding. VS Mac shows that old stuff like XAlign with no warning about it being deprecated.

    Like we see in VS for Windows.

    @MassimilianoBruni If it is at all an option, I would urge you to not us VS Mac if its going to do this sort of crap. If you have a Windows PC use that. If not, can you install VMware Parallels and run the Windows version of Visual Studio on your Mac?

  • MassimilianoBruniMassimilianoBruni USMember ✭✭
    edited December 2017
    I need to use the Mac version because I have to test on iOS. I could use a LAN Mac but this means I must have another Mac.
    I will wait that Xamarin team will solve the problem.
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I need to use the Mac version because I have to test on iOS. I

    Then you don't understand the ecosystem or how Xamarin works. You can build in Windows and test on the Mac. I do it every day. The Mac is just a build server. It needs to have XCode. But you don't have to do your development on it.

    I could use a LAN Mac but this means I must have another Mac.

    Again. No. Plenty of people have one machine. They virtualize the PC environment so they can use the grown-up Windows version of Visual Studio, and let the Mac OS do its thing for the build.

    I will wait that Xamarin team will solve the problem.

    So you have 2 years to just sit and make no progress?

    I'd suggest you not sit around and do nothing. Read up on Xamarin and get a better understanding of how it all works and re-educate yourself about those misconceptions. While you're at it - try something. Actually make an effort to get a working environment up and running on your Mac. Its not difficult to install Parallels so you can run Windows Visual Studio. You could be fully operational at a professional freelancer level before the weekend.

    Frankly... Honestly... And well... a bit harshly... If you're not the kind of guy to make an effort at even getting a working environment up then this might not be the best career path for you. If you're someone that gives up this easily and says "I'll wait around for someone else to fix it", development isn't going to be an industry that you find enjoyable or that you'll thrive in. And that's fine. I'm not being mean about it. Software development isn't for everyone any more than food service is. I have no business in food service or being a taxi driver or an flight control tower operator. I'm not suited to those jobs and there is no shame in admitting that one job or another doesn't mesh with one's abilities or natural tendencies.

  • Following your advices, I have done the following things:
    -LoginView XAML
    -RegistrationView XAML
    -A ViewModel
    -A XAML Page that contains a CarouselView, which has the ItemsSource and Position properties binded to the ViewModel. In the ViewModel's constructor I have created a initialized a DataTemplate List with two elements (LoginView and RegistrationView templates). This list is a ViewModel's Property.
    TODO:
    -Add button commands to switch between pages and go to the MainPage if Login is clicked (and if credentials are correct, which I have in plan to implement on a different class which handles database requests).

    Please tell me if I'm going to the right direction.

    @ClintStLaurent said:

    I need to use the Mac version because I have to test on iOS. I

    Then you don't understand the ecosystem or how Xamarin works. You can build in Windows and test on the Mac. I do it every day. The Mac is just a build server. It needs to have XCode. But you don't have to do your development on it.

    I could use a LAN Mac but this means I must have another Mac.

    Again. No. Plenty of people have one machine. They virtualize the PC environment so they can use the grown-up Windows version of Visual Studio, and let the Mac OS do its thing for the build.

    I will wait that Xamarin team will solve the problem.

    So you have 2 years to just sit and make no progress?

    I'd suggest you not sit around and do nothing. Read up on Xamarin and get a better understanding of how it all works and re-educate yourself about those misconceptions. While you're at it - try something. Actually make an effort to get a working environment up and running on your Mac. Its not difficult to install Parallels so you can run Windows Visual Studio. You could be fully operational at a professional freelancer level before the weekend.

    Frankly... Honestly... And well... a bit harshly... If you're not the kind of guy to make an effort at even getting a working environment up then this might not be the best career path for you. If you're someone that gives up this easily and says "I'll wait around for someone else to fix it", development isn't going to be an industry that you find enjoyable or that you'll thrive in. And that's fine. I'm not being mean about it. Software development isn't for everyone any more than food service is. I have no business in food service or being a taxi driver or an flight control tower operator. I'm not suited to those jobs and there is no shame in admitting that one job or another doesn't mesh with one's abilities or natural tendencies.

    I am an undergraduate student. I'm following a class where the goal is creating an app. The professor didn't teach us literally anything on how mobile development works (his lessons are not technical, because in his opinion it is "easy stuff"). No one told me what MVVM is. So now I have learned it, and I must say thank you that you suggested me to do it.
    I don't plan to be neither a mobile developer nor a freelancer. I only want to learn something. So, I have no intention to re-setup my environment because of a lack of Xamarin devs. I mean, they propose two possible environments to develop on, but I should use the Windows one because it is better? It's the same as going to McDonald's and hearing from the seller "you must eat a Big Mac because Crispy McBacon is not prepared properly". So why do you sell it? I'm not using a beta version, I'm using the official Mac version of Visual Studio (which comes from Xamarin Studio). Windows is not a problem, I have all the systems on my machine (macOS, Windows, GNU/Linux), but I'm not migrating on it because Xamarin devs are not capable to mark deprecated properties on Mac.
    My exams scores are high and I consider myself as capable, determined and interested in computer science.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Please tell me if I'm going to the right direction.

    Sounds pretty good so far.

    I should use the Windows one because it is better?

    Visual Studio for Windows has existed for nearly 2 decades. Or in other words Visual Studio for Windows has existed nearly as long as you have. The Mac version is only a year or two into development. Common sense will tell you that the Windows version used by professional software development houses for years will simply be more robust, more reliable, more feature-rich and so on.

    I'm not migrating on it because Xamarin devs are not capable to mark deprecated properties on Mac.

    Ok. Just realize that if you're following an up-to-date lesson and it tells you to do "abc" and you choose to do "def" just because something popped up in a system you've been told has issues... Its on you. Xamarin concentrates on Visual Studio 2017 Windows. If you want to use something else you're responsible for filtering out the differences.

  • AndreiMisiukevich_AndreiMisiukevich_ USMember ✭✭✭✭✭

    Hi)
    Try this
    nuget.org/packages/CardsView/ -- nuget
    github.com/AndreiMisiukevich/CardView -- source and samples

Sign In or Register to comment.