嘿所有我有以下代码应该来获取我的jpg图像并将其放在表单的背景上。但是,我看到的只是黑色背景。
Dim myBrush As New ImageBrush()
Dim image As New Image()
Dim grid As New Grid()
image.Source = New BitmapImage(New Uri("pack://application:,,,/WpfApplication1;component/Resources/1680-logoless.jpg"))
myBrush.ImageSource = image.Source
grid.Background = myBrush
这是我目前的XAML:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Margin="157,145,0,0"/>
<Label x:Name="label1" Content="Label" HorizontalAlignment="Left" Margin="68,59,0,0" VerticalAlignment="Top" Width="111"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="335,62,0,0"/>
<Button x:Name="button1" Content="Encrypt" HorizontalAlignment="Left" Height="17" Margin="335,123,0,0" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="toEncrypt" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="172" Margin="335,145,0,0"/>
<TextBox x:Name="toDecrypt" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="172" Margin="335,173,0,0"/>
<Button x:Name="button2" Content="Decrypt" HorizontalAlignment="Left" Height="23" Margin="335,201,0,0" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
有人看到我可能遗失的任何东西吗?
答案 0 :(得分:1)
在XAML中设置Grid的x:Name
属性,这将在MainWindow类中创建一个字段:
<Window ...>
<Grid x:Name="rootGrid">
...
</Grid>
</Window>
然后,不是创建新的Grid实例,而是设置在XAML中声明的背景:
rootGrid.Background = New ImageBrush(New BitmapImage(
New Uri("pack://application:,,,/Resources/1680-logoless.jpg")))