0
votes

I am trying to get a picker SelectedItem value for use on another page, but it doesn't seem to want to work. I found a "solution" here:Xamarin Picker selected item persisting on multiple pages but it doesn't want to work for me. When I try to use it, in say a label, I can't seem to get it to display. I can manually set the string, but not use the string of the actual selected item. I created the static class as directed:

public static class PickerPersist
{
    public static string pickerSelectedItem = "test";
}

and included the xmlns:local="clr-namespace:C971" and the label binding to Text="{Binding x:static local:PickerPersist.PickerSelectedItem}"and it correctly shows "test" when displaying the label. My question is, how do I get the string of the actual SelectedItem to show? The Picker itself is on another page, and I was able to get it to display correctly with var pickerItem = (Terms)termpicker.SelectedItem; I tried setting the PickerSelectedItem to Page1.termPicker.SelectedItem; but it comes back with the error "Page1.termPicker is inaccessible due to its protection level". I was able to get rid of the error by setting public static string Picked; on the previous page class, and then setting the variable pickerItem to Picked. However, it doesn't seem to carry across to the new page. If I put a break after setting pickeritem to Picked everything shows that it is indeed set correctly, but it shows as null in the PickerPersist and thus not showing on the new page.

I hope all this makes sense. Any help you can offer would be great!! Thanks! Full code available here: https://github.com/yax51/C971.git

Xaml where Picker lives:

ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             xmlns:local="clr-namespace:C971"
             x:Class="C971.Page1"
             Title="Term Selector">


    <ContentPage.Content>
        <StackLayout>

            <Picker x:Name="termPicker" 
                    ItemsSource="{Binding Terms}"
                    ItemDisplayBinding="{Binding TermName}"
                    Title="Select a term"
                    IsEnabled="True"
                    SelectedItem="{x:Static local:PickerPersist.PickerSelectedItem}"/>

Xaml.cs


    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        public static String Picked;
        public Page1()
        {
            InitializeComponent();

        }
    ...

        void TermGoClicked(object sender, EventArgs e)
        {
            var pickerItem = (Terms)termPicker.SelectedItem;


            if (pickerItem == null)
            {
                DisplayAlert("Error", "Please Select a term", "Ok");
            }
            else
            {
                Picked = pickerItem.TermName;

                Navigation.PushAsync(new TermPage());
            }



        }

Static class

public static class PickerPersist
    {
        public static string PickerSelectedItem = Page1.Picked;
    }

Label on new page

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             xmlns:local="clr-namespace:C971"            
             x:Class="C971.TermPage">
    <ContentPage.Content>
        <StackLayout>
            <Label x:Name="test" Text="{x:Static local:PickerPersist.PickerSelectedItem}"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

1
Hi @Patrick Davis are you want to show selected Picker value on next page?Chetan Rawat
please post the relevant code here - linking to an external site is not recommendedJason
@ChetanRawat Yes. I have tried several things. I tried setting PickerPersist class as not static, then using PickerPersist pickerPersist = new PickerPersist(); on the new page and was able to successfully print it in a Console.WriteLinebut unable to set it to the label text.Patrick Davis
@Jason apologies. Question updated with relevant code.Patrick Davis
Use Picker listener SelectedIndexChange, Create a view model update the property of view model when the selected item changed, and bind the text with label with view model property.Muhammad Adeel Shoukat

1 Answers

0
votes

Use Picker listener SelectedIndexChange, Create a view model update the property of view model when the selected item changed, and bind the text with label with view model property. – @Muhammad Adeel Shoukat

Did this and needed to add a ViewCell into the ListView and it worked.