locked
How to access MainPage.xaml controls from another page

    Question

  • i have an image element in the MainPage.xaml and a flyout in SettingsContract1.xaml, when the flyout item is clicked the filepicker is opened and i want to set them image source of the image in the Mainpage with the file picked from the filepicker. How can i access the image from SettingsContract1.xaml

    image in MainPage.xaml

    <Image x:Name="img" HorizontalAlignment="Left" Height="332" VerticalAlignment="Top" Width="778" Margin="349,406,0,0"/>

    menuflyout sub in SettingsContract1.xaml.vb

    Private Sub chooseFile_Click(sender As Object, e As RoutedEventArgs) Handles chooseFile.Click
        Dim filePicker As FileOpenPicker
        filePicker = New FileOpenPicker()
        filePicker.ViewMode = PickerViewMode.Thumbnail
        filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
        filePicker.FileTypeFilter.Add(".png")
        Dim file As StorageFile = Await filePicker.PickSingleFileAsync()
    
        Using filestream As Streams.IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
            Dim bitmapImage As Windows.UI.Xaml.Media.Imaging.BitmapImage = New BitmapImage
            bitmapImage.SetSource(filestream)
            img.Source = bitmapImage 'gives an error
        End Using
    
    End Sub

    Sunday, January 18, 2015 8:29 PM

Answers

  • Found the answer from stackoverflow

    Private Async Sub chooseFile_Click(sender As Object, e As RoutedEventArgs) Handles chooseFile.Click
        Dim mapImage As Frame = Window.Current.Content
        Dim mainPage As MainPage = mapImage.Content
        Dim filePicker As FileOpenPicker
        filePicker = New FileOpenPicker()
        filePicker.ViewMode = PickerViewMode.Thumbnail
        filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
        filePicker.FileTypeFilter.Add(".png")
        Dim file As StorageFile = Await filePicker.PickSingleFileAsync()
    
        Using filestream As Streams.IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
            Dim bitmapImage As Windows.UI.Xaml.Media.Imaging.BitmapImage = New BitmapImage
            bitmapImage.SetSource(filestream)
            mainPage.img.Source = bitmapImage
        End Using
    
    End Sub

    • Marked as answer by Jay_P85 Sunday, January 18, 2015 9:47 PM
    Sunday, January 18, 2015 9:47 PM

All replies

  • Found the answer from stackoverflow

    Private Async Sub chooseFile_Click(sender As Object, e As RoutedEventArgs) Handles chooseFile.Click
        Dim mapImage As Frame = Window.Current.Content
        Dim mainPage As MainPage = mapImage.Content
        Dim filePicker As FileOpenPicker
        filePicker = New FileOpenPicker()
        filePicker.ViewMode = PickerViewMode.Thumbnail
        filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
        filePicker.FileTypeFilter.Add(".png")
        Dim file As StorageFile = Await filePicker.PickSingleFileAsync()
    
        Using filestream As Streams.IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
            Dim bitmapImage As Windows.UI.Xaml.Media.Imaging.BitmapImage = New BitmapImage
            bitmapImage.SetSource(filestream)
            mainPage.img.Source = bitmapImage
        End Using
    
    End Sub

    • Marked as answer by Jay_P85 Sunday, January 18, 2015 9:47 PM
    Sunday, January 18, 2015 9:47 PM
  • Nice to see that and thanks for sharing.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 19, 2015 9:56 AM
    Moderator