Why picker value got erased when uploading picture

some1some1 Member ✭✭✭

In my form i have some entry ,2 pickers and i have a button for uploading image from gallery. if i upload image after selecting values from picker, it get erased. why this happens , any solution for this?


  • JarvanJarvan Member, Xamarin Team Xamurai
    edited May 14

    Does the 'picker' bind resource? Have you checked the itemSource model? And what's the code in the button click event? Please post the relevant code.

  • some1some1 Member ✭✭✭
    edited May 15

    Code in Button Click

    ` private async void UploadButton_Clicked(object sender, EventArgs e)
    if (!CrossMedia.Current.IsPickPhotoSupported)
    await DisplayAlert("Warning", "Picking a photo is not supported", "OK");

            var file = await CrossMedia.Current.PickPhotoAsync();
            if (file == null)
            imageToUpload.Source = ImageSource.FromStream(() => file.GetStream());
            var stream = file.GetStream();
            using (var streamReader = new MemoryStream())
                result = streamReader.ToArray();
  • some1some1 Member ✭✭✭

    @yelinzh Code of my form

    <ContentPage.ToolbarItems> <ToolbarItem x:Name="btnSave" Text="Save" Activated="OnSave" Order="Primary"/> </ContentPage.ToolbarItems> <ContentView> <ScrollView> <StackLayout> <Grid x:Name="gridLayout" HorizontalOptions="FillAndExpand"> <Grid.RowDefinitions> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="100"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"/> <ColumnDefinition Width="2*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Label Text="Name" Grid.Row="0" Grid.Column="0"/> <Entry x:Name="EName" HorizontalOptions="FillAndExpand" Grid.Row="0" Grid.Column="1" Text="{Binding ProductName}"/> <Label Text="Code" Grid.Row="1" Grid.Column="0"/> <Entry x:Name="ECode" HorizontalOptions="FillAndExpand" Grid.Row="1" Grid.Column="1" Text="{Binding ProductCode}"/> <Label Text="Product Category Type" Grid.Row="2" Grid.Column="0"/> <Picker x:Name="procattype" SelectedIndex="{Binding prodcattype_SelectedIndex}" Grid.Row="2" Grid.Column="1" /> <Label Text="Product Category" Grid.Row="3" Grid.Column="0"/> <Picker x:Name="procat" SelectedIndex="{Binding prodcat_SelectedIndex}" Grid.Row="3" Grid.Column="1" /> <Label Text="Description" Grid.Row="4" Grid.Column="0"/> <Editor x:Name="Editordescrip" BackgroundColor="WhiteSmoke" Grid.Row="4" Grid.Column="1" Text="{Binding Description}"/> <Image x:Name="imageToUpload" Aspect="AspectFit" HorizontalOptions="EndAndExpand" VerticalOptions="EndAndExpand" Grid.Column="2" Grid.RowSpan="3" BindingContext="{Binding Image}" /> <Button x:Name="uploadButton" Text="Upload Image" Clicked="UploadButton_Clicked" Grid.Row="4" Grid.Column="2"/> </Grid>

  • JarvanJarvan Member, Xamarin Team Xamurai

    Sorry, I can hardly reproduce your issue.
    Would you mind sharing a basic demo? It'll help to reproduce the problem and get a solution.

  • some1some1 Member ✭✭✭

    here is demo , if i click uploadbutton and comeback values in picker got cleared. why this happens


  • JarvanJarvan Member, Xamarin Team Xamurai

    How do you return to the previous page after selecting a photo?

  • some1some1 Member ✭✭✭

    @yelinzh after selecting image it automatically returns to previous page. in the demo i dont have images in gallery so that i pressed back button.

  • JarvanJarvan Member, Xamarin Team Xamurai

    I wrote a basic demo and it works well. What's the code of setting ItemSource to Picker?

    public partial class Page5 : ContentPage
        ObservableCollection<string> list = new ObservableCollection<string>();
        public Page5()
            picker.ItemsSource = list;
        private void Btn_Clicked(object sender, EventArgs e)
            Navigation.PushAsync(new MainPage());


        <Label Text="Picker"
                    HorizontalOptions="CenterAndExpand" />
        <Picker x:Name="picker" />
        <Button x:Name="btn" Clicked="Btn_Clicked"/>

Sign In or Register to comment.