【问题标题】:Setting background URI image not working设置背景 URI 图像不起作用
【发布时间】:2012-03-14 14:43:14
【问题描述】:

一个窗口有两个背景图像,具体取决于用户在窗口中所在的导航页面。这些图像位于我的项目文件夹中的位置:

/Resources/Images/MyImage1.jpg

如果我使用 IDE 选择固定的背景图像,则 xaml 文件将更新为:

<NavigationWindow.Background>
    <ImageBrush ImageSource="/Blah.MyApp;component/Resources/Images/MyImage1.jpg" />
</NavigationWindow.Background>

这样可以正确显示图像。

但是,由于我希望图像切换,我为 Window 创建了一个 ViewModel(它实现了 INotifyPropertyChanged),它公开了一个 Uri 属性,如下所示:

private readonly string _image1 = "pack://application:,,,/Resources/Images/MyImage1.jpg";
private readonly string _image2 = "pack://application:,,,/Resources/Images/MyImage2.jpg";

public MainNavWindowViewModel()
{
    SetImage1();
}

private Uri _backgroundImg;
public Uri BackgroundImg
{
    get
    {
        return _backgroundImg;
    }
    private set
    {
        _backgroundImg = value;
        OnPropertyChanged("BackgroundImg");
    }
}

public void SetImage1()
{
    BackgroundImg = new Uri(_image1);
}

public void SetImage2()
{
    BackgroundImg = new Uri(_image2);
}

在主窗口的 xaml 文件中,我已将 NavigationWindow.Background 替换为:

<NavigationWindow.Background>
    <ImageBrush ImageSource="{Binding Path=BackgroundImg, Mode=OneWay}" />
</NavigationWindow.Background>

并将 Windows 的 DataContext 设置为 ViewModel。

但是,此功能只是在我的窗口上显示黑色背景(当我注释掉将 ViewModel 数据绑定到窗口的行时,这是相同的行为)。

我将 ViewModel 设置为 Window 构造函数的第一行中的 Datacontext。如果我在上面设置断点,它会执行这些行并且看起来没问题。

任何想法发生了什么或我如何找出问题所在?

TIA

【问题讨论】:

    标签: c# wpf data-binding viewmodel


    【解决方案1】:

    您使用的是 .NET 3.5? 有一个错误,但您可以使用转换器来达到相同的效果:

    Public Class BitmapImageToImageConverter
        Implements IValueConverter
    
        Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
            Dim bitmapImage = TryCast(value, BitmapImage)
            Dim height As Double = 0
    
            Dim returnImage As New Image()
    
            If bitmapImage IsNot Nothing Then
                returnImage.Height = 15
    
                If parameter IsNot Nothing AndAlso Double.TryParse(parameter.ToString, height) Then
                    returnImage.Height = height
                End If
    
                returnImage.Source = bitmapImage
            Else
                ' Set a default image if none is given
                Dim uriString = "pack://application:,,,/Resources/Images/nopicture.png"
    
                Dim img As New BitmapImage()
                img.BeginInit()
                img.UriSource = New Uri(uriString)
                img.EndInit()
    
                returnImage.Source = img
            End If
    
            Return returnImage
        End Function
    
    
        Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
    End Class
    

    使用:

    <Window.Resources>
        <local:BitmapImageToImageConverter x:Key="imageConverter" />
    </Window.Resources>
    
    <ImageBrush ImageSource="{Binding Image, Converter={StaticResource imageConverter}}" />
    

    然后图像是您的视图模型中 System.Windows.Media.Imaging.BitmapImage 的属性。要加载它我有以下方法(这会在 /Resources 文件夹中加载嵌入的图像:

    Protected Sub LoadBitmapImageFromResource(ByVal fileName As String)
        Dim assName = Assembly.GetCallingAssembly().GetName().Name
        Dim uriString = "pack://application:,,,/" + assName + ";component/Resources/" + fileName
    
        Dim img As New BitmapImage()
        img.BeginInit()
        img.UriSource = New Uri(uriString)
        img.EndInit()
    
        Image = img
    End Sub
    

    标准高度设置为 15 像素(在我的实现中,我将转换器用于菜单项,因此它们非常小;)),但您可以将整数值作为 ConverterParameter 设置图像的高度。

    PS:很抱歉 VB.NET,但我的客户要求用 VB.NET 而不是 c# 编写此代码...

    【讨论】:

    • 感谢您的回复。现在这不再是问题,但我使用的是 .Net 4。
    【解决方案2】:

    没关系,我知道出了什么问题。图像资源的构建操作设置为 Embedded Resources,但这应该设置为 Resources。不幸的是,仅进行构建并不总是应用更改,因此需要重新构建或清理+构建。

    【讨论】:

      猜你喜欢
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2014-01-23
      • 1970-01-01
      • 2021-05-15
      • 2020-09-12
      相关资源
      最近更新 更多