Why picker value got erased when uploading picture

sree_dsree_d 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?

Answers

  • yelinzhyelinzh Member, Xamarin Team Xamurai
    edited May 14

    @sree_d
    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.

  • sree_dsree_d 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");
    return;
    }

            var file = await CrossMedia.Current.PickPhotoAsync();
            if (file == null)
                return;
    
            imageToUpload.Source = ImageSource.FromStream(() => file.GetStream());
    
            var stream = file.GetStream();
    
    
            using (var streamReader = new MemoryStream())
            {
                stream.CopyTo(streamReader);
                result = streamReader.ToArray();
            }
    
        }`
    
  • sree_dsree_d 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>

  • yelinzhyelinzh 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.

  • sree_dsree_d Member ✭✭✭

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

    https://drive.google.com/file/d/1bIUh9sP551LzjWpzrqP344f7Pfn7DPyB/view?usp=sharing

  • yelinzhyelinzh Member, Xamarin Team Xamurai

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

  • sree_dsree_d 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.

  • yelinzhyelinzh Member, Xamarin Team Xamurai

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

    public partial class Page5 : ContentPage
    {
        ObservableCollection<string> list = new ObservableCollection<string>();
        public Page5()
        {
            InitializeComponent();
    
            list.Add("model_1");
            list.Add("model_2");
            list.Add("model_3");
    
            picker.ItemsSource = list;
        }
    
        private void Btn_Clicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new MainPage());
        }
    }
    

    page.xaml

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

Sign In or Register to comment.