2017-09-14 3 views
-1

Folgendes Bild zeigt, was mein Projekt ist.Fehler bei Option Strict auf

My project

Sie können meine Projektcodes in der folgenden für Ihre Prüfaufgaben finden.

<Window x:Class="MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Name="MainWindow" 
    Height="300" 
    Width="400"> 
<Grid> 
    <Image Name="Image1" Height="100" Width="125" HorizontalAlignment="Left"/> 
    <Button Name="Button1" Content="Button1" Height="30" Width="125" HorizontalAlignment="Center"/> 
    <Button Name="Button2" Content="Button2" Height="30" Width="125" HorizontalAlignment="Right"/> 
</Grid> 
</Window> 

...

Class MainWindow 

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click 
    Dim myOFD As New Microsoft.Win32.OpenFileDialog 
    myOFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 
    myOFD.Filter = "Image File (*.png)|*.png" 
    If myOFD.ShowDialog = True Then 
     Dim myBitmap As New BitmapImage 
     myBitmap.BeginInit() 
     myBitmap.UriSource = New Uri(uriString:=myOFD.FileName, UriKind:=UriKind.RelativeOrAbsolute) 
     myBitmap.EndInit() 
     myBitmap.Freeze() 
     Image1.Source = myBitmap 
    End If 
End Sub 

Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click 
    Dim myBitmapEncoder As New PngBitmapEncoder() 
    myBitmapEncoder.Frames.Add(BitmapFrame.Create(Image1.Source)) 
End Sub 

End Class 

ich folgende Fehler habe, wenn Option Strict auf.

'Public Gemeinschafts-Funktion erstellen (Quelle Als System.Windows.Media.Imaging.BitmapSource) Als System.Windows.Media.Imaging.BitmapFrame': Option Strict On nicht zulässt, implizite Konvertierungen von System.Windows.Media.ImageSource 'zu' System.Windows.Media.Imaging.BitmapSource '.

...

'Public Gemeinschafts-Funktion erstellen (bitmapStream Als System.IO.Stream) Als System.Windows.Media.Imaging.BitmapFrame': Der Wert des Typs ‚System.Windows.Media .ImageSource 'kann nicht in' System.IO.Stream 'konvertiert werden.

...

'Public Gemeinschafts-Funktion erstellen (bitmapUri Als System.Uri) Als System.Windows.Media.Imaging.BitmapFrame': Der Wert des Typs ‚System.Windows.Media.ImageSource System.Uri

+0

Nun, der Fehler sagt alles. Mit OPTIONS SICT ON müssen alle hinter den Kulissen stattfindenden Castings von Ihnen erledigt werden! –

+0

Was hat das mit C# zu tun? –

Antwort

1

Mit der Option Strict Option ‚kann nicht konvertiert werden‘ Sie Uri zum BitmapFrame.Create Methode für Ihren Code passieren muss selbst kompilieren:

Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click 
    Dim source = DirectCast(Image1.Source, BitmapImage) 
    Dim myBitmapEncoder As New PngBitmapEncoder() 
    myBitmapEncoder.Frames.Add(BitmapFrame.Create(source.UriSource)) 
End Sub 
+0

Ja, wahrscheinlich: https://stackoverflow.com/questions/3056514/difference-between-directcast-and-ctype-in-vb-net – mm8