WPF: “Value of type 'String' cannot be converted to 'System.Windows.Media.ImageSource'.”

后端 未结 5 1918
没有蜡笔的小新
没有蜡笔的小新 2021-01-12 16:42

I\'m trying to set a WPF Image\'s source.

XAML works:


<         


        
相关标签:
5条回答
  • 2021-01-12 17:16

    It can be even easier than the above:

    ImageThing.Source = New BitmapImage(New Uri("images/Thing.png", UriKind.Relative))

    0 讨论(0)
  • 2021-01-12 17:22

    What about

    Private Sub Google_Click(sender As Object, e As RoutedEventArgs)
        Mainbrowser.Navigate("http://www.contoso.com")
    
    0 讨论(0)
  • 2021-01-12 17:29

    WPF uses an implicit type converter to convert the xaml string to the expected type. In code you are statically bound by the object type... If you look at the example here it shows how to set the source property to a BitmapImage that is generated from a local uri programatically.

    0 讨论(0)
  • 2021-01-12 17:33

    Just change xaml file like this.

    <Image Name="ImageThing">
        <Image.Source>
           <BitmapImage UriSource="images/Thing.png" />
        </Image.Source>
    </Image>
    
    0 讨论(0)
  • 2021-01-12 17:36

    you will probably need to do something like this

    Uri i = new Uri("images\\Thing.png");
    

    keep in mind that you need to use a \ not a / for a windows file system

    Take a look here

    0 讨论(0)
提交回复
热议问题