1

I have a MainWindow Class (from Window) and there I'm trying to set Overlay icon on my taskbar.

this.TaskbarItemInfo.Overlay = (ImageSource)Resources["WarningImage"];

No matter what I try the Resources are empty (count 0) even though I added an image object in xaml. Is there a reliable way of setting the Overlay icon bypassing resources, loading image from a file? Is there a way to reference resources in ResourceDictionary of an application so that I can set the Overlay using that approach.

// Resources are null no images found

var test = (DrawingImage)this.FindResource("WarningImage");
this.TaskbarItemInfo.Overlay = (ImageSource)Resources["WarningImage"];

Thank you, ~r

 <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Styles/AppStyles.xaml"/>
                <ResourceDictionary>
                    <DrawingImage x:Key="WarningImage">
                        <DrawingImage.Drawing>
                            <ImageDrawing Rect="0,0,16,16" ImageSource="/Images/question.png" />
                        </DrawingImage.Drawing>
                    </DrawingImage>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
Radek
  • 133
  • 1
  • 9

1 Answers1

0

The problem is caused since you’re creating a resource dictionary. Just keep it in Window.Resources like this

<Window.Resources>
    <DrawingImage x:Key="WarningImage">
        <DrawingImage.Drawing>
            <ImageDrawing Rect="0,0,16,16" ImageSource="/Images/question.png" />
        </DrawingImage.Drawing>
    </DrawingImage>
</Window.Resources>

Access the resource this way :

var test = (DrawingImage)this.FindResource("WarningImage");
var test1 = (DrawingImage)this.FindName("WarningImage");
ViVi
  • 4,339
  • 8
  • 29
  • 52
  • I need to pull in some shared resources from Styles and the only way I could do that was via ResourceDictionary. If I add DrawingImage together with ResourceDictionary it will not compile. Is there a way of adding an image to resources and accessing shared resources at the same time? I tried to move the image to the shared resource but that wasn't working well either. Is there a way to access ResourceDictionary to retrieve the image? – Radek Jul 27 '16 at 15:17
  • @Radek : Yes you can move the resource dictionary to a separate file, maybe **StyleDictionary.xaml** and it is possible to access the resources from that file. Why don't you simply add the image to a folder as a resource and use it in your project? – ViVi Jul 27 '16 at 17:56
  • The resource way was a simplest way I found for me to get an instance of ImageSource object that I needed to set TaskbarItemInfo.Overlay – Radek Jul 29 '16 at 03:41
  • @Radek : Give this a read : http://stackoverflow.com/questions/92100/is-it-possible-to-set-code-behind-a-resource-dictionary-in-wpf-for-event-handlin?rq=1 – ViVi Jul 29 '16 at 03:44