簡體   English   中英

圖像控件未呈現

[英]Image control doesn't get rendered

我正在嘗試使用多個小型編輯器創建WPF應用程序。 這些編輯器之一需要加載兩個圖像,在TextBox中輸入名稱,然后單擊保存按鈕。

在代碼中,這確實可以完美地工作。 文件將保存在模型中,並且可以加載圖像。 在點擊保存按鈕之前,兩個圖像實際上都顯示在編輯器中。 但是,重新打開(用於編輯)后,僅渲染一張圖像。

我進行了一些測試,發現總是沒有渲染第一張圖像,而第二張卻總是。

例如,在XAML中,它看起來像這樣:

<Image Name="BackgroundImage" Grid.Row="1" Grid.Column="0" Source="{Binding Path=Background}" Width="120" Height="90"/>
<Image Name="ForegroundImage" Grid.Row="2" Grid.Column="0" Source="{Binding Path=Foreground}" Width="120" Height="90"/>

這里的BackgroundImage沒有得到呈現,即使該模型的屬性Background已成功加載圖像。 如果我要交換這些XAML標記,這意味着將ForegroundImage控件放在BackgroundImage之上,那么ForegroundImage不會被渲染,而BackgroundImage卻會被渲染。 即使我不更改其他任何東西,例如Grid.Row或Column。

然后,我嘗試在窗口的Loaded事件處理程序中的代碼后面加載圖像:

private void LocationEditor_OnLoaded(object sender, RoutedEventArgs e)
{
    BackgroundImage.Source = ((Location)DataContext).Background;
    ForegroundImage.Source = ((Location)DataContext).Foreground;
}

同樣適用於這種情況。 無論哪一行先執行,都不會在Window中呈現。

如果有幫助,下面是Background屬性的代碼( Foreground的構建方式相同):

[JsonIgnore]
public BitmapImage Background
{
    get
    {
        if (!string.IsNullOrWhiteSpace(BackgroundFile))
        {
            SetFree();
            SetImage();
        }
        else
            _background = null;
        return _background;
    }
}

如果不再需要該映像,則SetFree()方法將釋放內存資源。 當窗口關閉或需要BitmapImage時,就會發生這種情況。 (如果圖像文件已更改,它將在每次重新加載圖像。)

SetImage()方法僅做一件簡單的事情:加載BackgroundFile圖像文件的圖像並將其保存在_background字段中。


我不太清楚問題可能是什么。 我已經嘗試了一些方法,但是在編碼時我並不經常使用圖像。

您的SetFree()SetImage()方法一定存在問題。

幸運的是,由於WPF提供了從stringUribyte[]ImageSource內置自動類型轉換,因此您的視圖模型中根本不需要BackgroundForeground屬性。 因此,您可以將Image的Source屬性直接綁定到視圖模型中的相應路徑屬性:

<Image ... Source="{Binding BackgroundFile}"/>
<Image ... Source="{Binding ForegroundFile}"/>

為了完整起見,如果您仍然想要擁有這些屬性,我建議使用如下所示的簡單實現。 內置圖像緩存將注意不要對文件進行過多的解碼:

public ImageSource Background
{
    get
    {
        if (BackgroundFile != null)
        {
            try
            {
                return new BitmapImage(new Uri(BackgroundFile));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        return null;
    }
}

綁定問題可能很難解決。 有時,評估綁定的時機不明顯或似乎沒有道理。 我知道這是一個通用答案,但是我使用的一個技巧是為我遇到麻煩的綁定添加一個“調試轉換器”,以便可以在評估期間進行調試。 它救了我幾次。

<UserControl
 xmlns:converters="clr-namespace:WhateverYourNamespaceIs.Converters">
<UserControl.Resources>
<converters:DebugConverter x:Key="DebugConverter"/>
</UserControl.Resources>

<Image Name="BackgroundImage" Grid.Row="1" Grid.Column="0" Source="{Binding Path=Background, Converter={StaticResource DebugConverter}" Width="120" Height="90"/>




Here is an example of the converter.

public sealed class DebugConverter : IValueConverter
    { 


        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //Debug.WriteLine("Debug Converter Value:" + value.ToString());
            // Since you are working with graphics, maybe just dump the size
Debug.WriteLine("Debug Converter Value:" + value.Length().ToString());
            return (value);
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM