我在MainPage.xaml中有一个图像元素,在SettingsContract1.xaml中有一个弹出按钮,当点击弹出项目时,打开了文件选择器,我想在主页面中设置图像的图像源,并从中选取文件filepicker。如何从SettingsContract1.xaml
访问图像 MainPage.xaml中的图像
<Image x:Name="img" HorizontalAlignment="Left" Height="332" VerticalAlignment="Top" Width="778" Margin="349,406,0,0"/>
SettingsContract1.xaml中的menuflyout子
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
答案 0 :(得分:0)
您可以创建一个映射到MainPage
的变量,然后访问那里的任何公共成员表单。
Dim mp = TryCast(Application.Current.RootVisual, MainPage)
If Not mp Is Nothing Then
mp.img.Source = bitmapImage
End If
答案 1 :(得分:0)
找到答案here并将其转换为VB
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