2017-09-14 29 views
-1

下圖顯示了我的項目是什麼。選項嚴格時的錯誤

My project

您可以在您的測試需求下面我的項目代碼。

<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 

我有以下錯誤選項時嚴格。

「公共共享功能創建(源作爲System.Windows.Media.Imaging.BitmapSource)作爲System.Windows.Media.Imaging.BitmapFrame」:選項嚴格On不允許從System.Windows.Media.ImageSource隱式轉換'到'System.Windows.Media.Imaging.BitmapSource'。

...

'公共共享功能創建(bitmapStream作爲System.IO.Stream)作爲System.Windows.Media.Imaging.BitmapFrame' 類型的值「System.Windows.Media .ImageSource'不能轉換爲'System.IO.Stream'。

...

'公共共享功能創建(bitmapUri作爲的System.Uri)作爲System.Windows.Media.Imaging.BitmapFrame':類型System.Windows.Media.ImageSource的」價值「不能轉換爲」的System.Uri

+0

那麼錯誤說明了一切。隨着選項Stict開啓,所有幕後演員都必須由您完成! –

+0

這與C#有什麼關係? –

回答

1

隨着Option Strict選項,您必須通過UriBitmapFrame.Create方法爲您的代碼無法編譯:

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

是的,可能是:https://stackoverflow.com/questions/3056514/difference-between-directcast-and-ctype-in​​-vb-net – mm8