Компонент OpenFileDialog в Visual Studio 2017 - PullRequest
0 голосов
/ 29 июня 2018

Я работаю над проектом VB в Visual Studio 2017. Это проект Blank App (Universal Windows). При попытке работать с этим типом приложения у него, похоже, нет OpenFileDialog, как у приложения Windows Forms (.NET Framework). Есть ли способ сделать одну из двух вещей:

  1. Создайте приложение Windows Forms, которое будет выглядеть так же, как и пустое приложение (Universal Windows)

  2. Добавить опцию OpenFileDialog в пустое приложение (универсальный Windows)

Ответы [ 2 ]

0 голосов
/ 13 июля 2018

Следующий код является VB-версией примера MS на https://docs.microsoft.com/en-us/uwp/api/Windows.Storage.Pickers.FileOpenPicker.

Imports Windows.Storage

Imports Windows.Storage.Pickers

Public NotInheritable Class MainPage
    Inherits Page

    Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim openPicker As New FileOpenPicker()
        openPicker.ViewMode = PickerViewMode.Thumbnail
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
        openPicker.FileTypeFilter.Add(".jpg")
        openPicker.FileTypeFilter.Add(".jpeg")
        openPicker.FileTypeFilter.Add(".png")

        Dim file As StorageFile = Await openPicker.PickSingleFileAsync()

        If (file IsNot Nothing) Then
            Debug.WriteLine($"Picked File: {file.Name}")
        Else
            Debug.WriteLine("Operation Cancelled.")
        End If

    End Sub
End Class
0 голосов
/ 12 июля 2018

То, что я в итоге сделал, чтобы получить такой же (ish) вид, было просто поиграться с некоторыми свойствами формы и кнопок. Это дало мне вид, что я был после. Это не было точно, но я возьму это.

Что касается OpenFileDialog, я использовал следующее:

    Dim myStream As IO.Stream = Nothing
    Dim openFileDialog1 As New OpenFileDialog()

    ' Open file dialog parameters
    openFileDialog1.InitialDirectory = "c:\"                    ' Default open location
    openFileDialog1.Filter = "Executable Files (*.exe)|*.exe|All Files (*.*)|*.*"   
    openFileDialog1.FilterIndex = 2
    openFileDialog1.RestoreDirectory = True

    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Try
            myStream = openFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ' Insert code to read the stream here.
                Textbox1.Text = openFileDialog1.FileName

                ' Even though we're reading the entire path to the file, the file is going to be ignored and only the path will be saved.
                ' Mostly due to me lacking the ability to figure out how to open just the directory instead of a file. Resolution threadbelow.
                ' http://www.vbforums.com/showthread.php?570294-RESOLVED-textbox-openfiledialog-folder

                ' Setting the public variable so it can be used later
                Dim exepath As String
                exepath = IO.Path.GetDirectoryName(Me.txtExeLocation.Text)

            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally
            ' Check this again, since we need to make sure we didn't throw an exception on open.
            If (myStream IsNot Nothing) Then
                myStream.Close()
            End If
        End Try
    End If
...