Image shows in Expression Blend but not during runtime - silverlight

The image in question is located in a ControlTemplate inside of a ResourceDictionary similar to this (various details removed for clarity):
<ResourceDictionary
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">
<ControlTemplate x:Key="ImageTestTemplate" TargetType="ImageTest">
<Grid>
<Image Source="/MyAssembly;Component/Images/MyImage.png"/>
</Grid>
</ControlTemplate>
</ResourceDictionary>
When I view the control in Expression Blend the image shows up just fine, but when I run the app the image does not show up. If I drop the same image into a UserControl it also shows up just fine, so the problem has something to do with using the template.
Is there some other way I should be loading the image resource inside the template?

I don't think you need the leading '/'. The base resource path I'm using for a project is "IMLPrototype2;component/Model/Sounds/" in SL 3 & 4.

Related

Is it valid to use a Blend d:Style.DataContext inside WPF resource dictionary?

When defining WPF styles, I often use the Expression Blend <d:Style.DataContext> tag to let Intellisense know what the runtime DataContext will be. It works great. Unfortunately I cannot seem to make this work in a resource dictionary and I am unclear as to why.
For example, here in a style for a RadTabItem, I tell Intellisense that SettingsPageVm will be the DataContext:
<UserControl x:Class="Views.ConfigureView"
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:tk="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:viewModels="clr-namespace:ViewModels"
d:DataContext="{d:DesignInstance viewModels:ConfigureVm}">
<Grid>
<tk:RadTabControl ItemsSource="{Binding Categories}" >
<tk:RadTabControl.Resources>
<!-- Tell setters that SettingsPageVm will be the datacontext -->
<Style TargetType="{x:Type tk:RadTabItem}">
<d:Style.DataContext>
<x:Type Type="viewModels:SettingsPageVm" />
</d:Style.DataContext>
<!-- (Imagine many setters here, binding to SettingsPageVm) -->
This works fine.
But if try I move that same style to a separate ResourceDictionary Visual Studio complains. The editor puts a blue squiggly underneath the d:Style.DataContext declaration and highlights it red. The compiler complains about it as follows:
1>I:\Dev\MyApp\Resources\Styles\DialogStyles.xaml(13,14): error
MC4004: Style cannot contain child 'TypeExtension'. Style child must
be a Setter because it is added to the Setters collection. Line 13
Position 14.
Why is this valid in one context and not valid in another?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModels="clr-namespace:ViewModels"
xmlns:tk="http://schemas.telerik.com/2008/xaml/presentation"
>
<Style x:Key="SettingsPageTabStyle" TargetType="{x:Type tk:RadTabItem}">
<d:Style.DataContext> <!-- *** ERROR *** -->
<x:Type Type="viewModels:SettingsPageVm" />
</d:Style.DataContext>
</Style>
</ResourceDictionary>
Answering my own question because half a day later I stumbled on it.
I had neglected to add the
mc:Ignorable="d"
line to my XAML. Once I did that, everything worked fine.
(Just One of those obscure WPF lessons that I am sure I already learned and then forgot at least once before -- maybe twice -- in the past over several years of working on WPF. )

IOException when referencing App.xaml's ResourceDictionary

I'm trying to reference App.xaml's ResourceDictionary from a separate WPF window. I want to use resources from there in different windows, and this seems like the recommended way to do it. Unfortunately, I don't seem to be able to effectively reference App.xaml from the other window. Here is my App.xaml:
<Application x:Class="View.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ViewModel="clr-namespace:ViewModel;assembly=ViewModel"
xmlns:local="clr-namespace:View"
StartupUri="ClockView.xaml">
<Application.Resources>
<ResourceDictionary>
<local:PriorityToIconConverter x:Key="PriorityToIconConverter" />
</ResourceDictionary>
</Application.Resources>
Notes: I'm not using MainWindow, so I've replaced the startup URI with a form that always comes up. I noted that in some other answers, the location of MainWindow is sometimes the issue. In my case, I haven't seen any difference between using ClockView or MainWindow. Both ClockView and MainWindow exist in the root namespace, MainWindow is just never loaded. I also have more resources, but I've removed them for the sake of conciseness.
Here's a simplified example of the code where I'm trying to reference the ResrouceDictionary from App.xaml:
<local:AssistantWindow
x:Class="View.AutomatorView"
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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:properties="clr-namespace:View.Properties"
xmlns:local="clr-namespace:View"
mc:Ignorable="d"
Title="Tool"
x:Name="Tool"
Background="Transparent"
Height="600"
Width="450"
Topmost="{Binding Source={x:Static properties:Settings.Default}, Path=ToolAlwaysOnTop}"
MinHeight="515"
MinWidth="150">
<Window.Resources>
<ResourceDictionary Source="App.xaml" />
</Window.Resources>
Again, this is simplified to be concise. When I try to load this form, I get the exception:
System.Windows.Markup.XamlParseException: ''Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '21' and line position '10'.'
Inner Exception
IOException: Cannot locate resource 'tool/app.xaml'.
The view for "Tool" is located in a folder that is also named "Tool." However, the xaml and code behind don't reference this namespace, I'm just using the folder to organize my classes. It looks like it's looking for App.xaml in the folder the view resides in. App.xaml resides in the root namespace (View). I've tried modifying the source in the xaml for Tool to:
- View.App.xaml
- View:App.xaml
- View/App.xaml
How can I get this reference to work, so I can share resources throughout my application? Thank you.
You can't load App.xaml like you're trying to do because it's not actually a ResourceDictionary. You can only specify ResourceDictionary files as the target of Source.
However, if you declare a resource in App.xaml, you can reference it anywhere without needing to load the file it's in. That's done for you automatically. Therefore, you can reference your converter at any time with {StaticResource PriorityToIconConverter}.
Note that if you moved it from the default starting location (the base project folder) you may have to update its location. Right click your project, then Properties. Navigate to the "Application" tab (should be the uppermost element on the left-hand sidebar) and look for the "Startup object" field. Set that to [ProjectName].[Namespace?].[Namespace?].App. When I tested it, mine worked without needing to manually change the location, but your setup may be different.

WPF: MediaElement doesn't display at runtime

This concerns WPF. The problem is that my MediaElement (actually a GIF) doesn't show up at runtime (so I only get an empty screen), even though it shows perfectly in the design mode. Before citing the code I note the following:
The GIF-file in question has been added to the solution.
Its BuildAction property is set to Resource (I've checked).
When I replace the MediaElement by an Image element (and either use the same GIF-file or replace that source-file by a .png file), the image/GIF does display at runtime. Just not when it's used as a MediaElement.
Closing and re-starting Visual Studio doesn't help.
As I said (and just to emphasize, if I may), the GIF image does display in the design mode part of the screen - just not at runtime.
And here's the code:
<Window x:Class="Testing.TestWindow"
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:Testing"
mc:Ignorable="d"
Title="TestWindow" WindowState="Maximized">
<StackPanel>
<MediaElement Source="pack://application:,,,/Images/untitled.gif" Stretch="Fill" Visibility="Visible" />
</StackPanel>
</Window>
Thanks.

Silverlight Image element not showing up

Silverlight newbie question:
I've got a small Silverlight application with the following XAML snippet:
<Image Source="http://judahhimango.com/images/smileys/cool.gif" />
The image doesn't show up! I would expect the image to download asynchronously, then show up in the UI, but alas, no such luck.
What am I doing wrong?
Update -------------------------------------------------
Ok, no-go on the .GIFs, forgot about that. But using a PNG doens't work either:
<Image Source="http://judahhimango.com/images/smileys/cool.png" />
However, if I change the Source to a BitmapImage, then it works:
<Image>
<Image.Source>
<BitmapImage UriSource="http://judahhimango.com/images/smileys/cool.png" />
</Image.Source>
</Image>
Why can't I just specify the Image Source? Why do I have to specify a BitmapImage?
I changed two properties on my png image: Build Action to Content and Copy to Output Direct Copy always.
I think SL does not support gifs:
http://forums.silverlight.net/forums/p/3883/232781.aspx
can you try a jpg?
3/9/10: Editing per request:
I can't post complete code, but here is my MainPage.xaml
<UserControl x:Class="SilverlightApplication2.MainPage"
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"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<Image Source="http://judahhimango.com/images/smileys/cool.png"/>
</Grid>
</UserControl>
3/9/10: OK, I think I know what your problem may be. When you create a new silverlight application project in Visual Studio, it gives you the option of hosting it in an ASP.NET website. Did you select that option? If not, please select it, and give it a try. See this:
deep zoom is not displayed

Load Silverlight Canvas as a seperate XAML File?

I want to be able to store various Canvas items in seperate XAML files so for example they are declared as:
<canvas x:Class="Item.One" Height="300" Width="400">
...
</canvas>
and another like this
<canvas x:Class="Item.Two" Height="300" Width="400">
...
</canvas>
I am wondering why I cannot get this to work when I try and load them in as classes I get a parser error, I can do this fine in WPF but not in Silverlight 3.0, what can you do to have to have XAML work as objects rather than resources?
Just to help the Parser error is
AG_E_PARSER_BAD_TYPE
And a real example that does not work:
<Canvas x:Class="Cards.Appointment.ZuneVertical" x:Name="ZuneVertical"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="240" Height="320" Background="White">
</Canvas>
In my code I want to do this:
Preview.Children.Add(Item.One)
Where Preview is a Grid within a ScrollView which is where I want the XAML to be loaded into, ie the Canvas, there a various versions of Canvas I want to load into this Preview Pane, each one is a Class as XAML in WPF, but cannot seem to get this to work in Silverlight 3.0 without the parsing error, tried UserControls but this has the same problem!
Seems it was a problem with the Namespace of my XAML file I added the Application Namespace and this resolves the problem.
<Canvas x:Class="ZuneCardrintouch.Cards.Appointment.ZuneVertical" />

Resources