Weird UserControl image path behaviour in Blend 4 and WPF 4.0 - wpf

I'm using Blend 4 and VisualStudio 2010 to build a kiosk WPF 4.0 app that has a wizard-like function. I've decided I'd like individual steps moved to separate UserControls, to keep the main window of the application as lean as possible. However, I'm having trouble including the UserControl into the main window, since image paths are being mangled after moving the XAML to a separate UserControl file.
This code works when it's put into the main window XAML:
<Button Grid.Row="1" Grid.Column="2" Height="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Margin="20" VerticalAlignment="Bottom" Style="{DynamicResource ImageButton}">
<Button.Background>
<ImageBrush ImageSource="/Images/Extra/motor_boat_2.jpg" Stretch="UniformToFill"/>
</Button.Background>
</Button>
But when I move that code to a separate user control and include it in the main window, the designer throws and exception:
IOException: Cannot locate resource 'images/extra/sailing_boat.jpg'.
I've tried using a couple of different syntaxes, but all fail, sometimes using the %ProgramFiles% directory, other times converting the absolute path to relative (as above). Sometimes, the Build Project operation in Blend 4 fails because of this.
All the images are marked as Resource with Build Action set to Copy Always.
VisualStudio has no problem building and running the application, the UserControl displays correctly within the running application with no thrown exceptions.
EDIT:
Using relative paths in the UserControl and then building and running the app from VisualStudio and switching back to Blend correctly displays the images, until the next modification to the UserControl.

Use Pack URI like following:
Uri uri = new Uri("pack://application:,,,/Images/Extra/motor_boat_2.jpg");

Related

Why does my app ignore certain xaml code on Windows 7?

I'm creating a WPF C# .Net App with the Aero theme which is supposed to run on Win7 as well as Win10.
On Win10 everything works as it's supposed to, however on Win7 the GUI looks vastly different.
In particular, my MenuItems simply seem to ignore certain settings such as VerticalContentAlignmentand BorderThickness.
Pics:
Win7 vs
Win10
How do I fix this?
(Some Code:)
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
<MenuItem Header="Intranet"
Grid.Column="2"
x:Name="Menu_Intranet"
ToolTip="Intranet"
BorderBrush="white"
BorderThickness="5"
VerticalAlignment="Stretch"
Click="Menu_1_Click"
ToolTipService.ShowOnDisabled="true"
VerticalContentAlignment="Center"/>
Thanks in advance!
I edited the MenuItemControlTemplate (right click on the control you want to edit in the designer -> edit template) and applied it to the MenuItems via Template="{DynamicResource MyMenuItemControlTemplate}".
I guess this leaves no room for the OS/Theme to apply its own settings.

Set default xaml window background image in dll

I am currently developing a default WPF control Kit.
But I am stuck with using the correct kind of uri in xaml.
What I have is an image wich should be used as the background for the non-client area of my window.
To make the default controls available very easy I want to put everything in a dll.
Other apps can quickly reference that dll and get access to the style.
The problem is, that my image is not showing up when using the dll style in an app.
My image (/Resources/WindowBackground.jpg) is set to Resource and I am using the following xaml:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99">
<Image.OpacityMask>
<ImageBrush ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
</Image.OpacityMask>
</Image>
I also tried:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99" Source="/Resources/WindowBackground.jpg"/>
Both write the following into the output (Couple times):
..."System.IO.IOException" in PresentationFramework.dll...
I also tried lots of other uris wich sometimes lead to XamlParseExeptions and other not so nice stuff.
Thank you for any hints :D
You can find your answer in the Pack URIs in WPF page on MSDN. For your particular situation, you can use the following syntax to reference your resource image file:
pack://application:,,,/ReferencedAssembly;component/Resources/WindowBackground.‌​jpg
From the linked page:
The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
Note: The type of resource file here is irrelevant.

Is WPFtoolkit compatible with .Net 4.5?

I get this error when running the application.
XmlParseException Occured
Could not load file or assembly 'System.Windows.Controls.DataVisualization.Toolkit, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified
XAML
<UserControl x:Class="myNamespace.Views.ChartView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chart="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Label Margin="10" Content="Scatter series" />
<chart:ScatterSeries DependentValueBinding="{Binding Value}"
IndependentValueBinding="{Binding Key}"
ItemsSource="{Binding PlotValues}" />
</Grid>
ViewModel:
public ObservableCollection<KeyValuePair<double, double>> PlotValues
{
get { return _plotValues; }
set { _plotValues = value; }
}
I think your problem is related to an issue/bug with the dependency detection in Visual Studio or MSBuild (related bug can be found here).
I've been able to reproduce your problem by doing the following:
Create a new WPF application
Create a new UserControl Library
Install the System.Windows.Controls.DataVisualization.Toolkit NuGet package on the UserControl Library (not the WPF application)
Add the scatter series
Add the custom user control containing the scatter series to the main window and running the application
The problem seems to be that without any reference to anything in the code (outside XAML), Visual Studio assumes that you are not using the assembly and therefore does not copy it to the output directory of the main application/project referencing it. More details can be found here, although these are references in ordinary code (C#, not XAML).
There are three workarounds which seems to make Visual Studio detect the dependency which I know of:
Give the control a name, and any name will do (my preferred solution)
Add a C# code reference to a controller in the Control Library (dummy code)
Add the NuGet or .dll reference to the actual application which is run
After having struggled with the same issue myself for a few hours, I believe to have discovered that Visual Studio is unable to detect the dependency on the dll based on a single usage directly in the XAML without a code reference (or a Name, which creates a code reference for you, allowing you to access the controller in the code-behind).
Edit:
I have reported this issue to Microsoft through Connect, so that they hopefully will fix it.

Controls don't show over Winforms Host

I am trying to load a swf file as background for my WPF window. For this I have used a WinformHost and I load the swf movie in the Winform host using the plugin AxShockwaveFlashObjects.
<Grid>
<WindowsFormsHost Name="wfh">
<ax:AxShockwaveFlash x:Name="axFlash"/>
</WindowsFormsHost>
</Grid>
Till here the application works fine. However when I add my other controls(buttons,textblocks etc) to the Grid, they dont show. All I see is just the movie. Any pointers please.
This is actually expected since the WPF elements are all rendered within a single HWND (that of the WPF Window in this case) and therefore are below the WindowsFormsHost (or any other HwndHost). This is discussed here in MS' documentation and also here.
In theory this will be supported by some new functionality being added to .NET 4.5 - via the IsRedirected property of the HwndHost. This is discussed in some of the preview documentation for 4.5 here.

Image in ComboBox only visible in designer

Here's the situation:
I have a combobox within a button,, In expression blend when I add image to combo box item with the cod below, there's no problem apparently, image and textblock are showing well but as I run the proj.((F5)) there'e no sign of image, What I'm doing Wrong???
here's the code I use to add the image:
<ComboBoxItem Background="LightCoral" Foreground="Red"
FontFamily="Verdana" FontSize="12" FontWeight="Bold">
<CheckBox Name="CoffieCheckBox">
<StackPanel Orientation="Horizontal">
<Image Source="coffie.jpg" Height="30"></Image>
<TextBlock Text="Coffie"></TextBlock>
</StackPanel>
</CheckBox>
Any help Is much appreciated...
One likely problem might be that the image file is in your project folder without being copied to the build folder; /bin/Release for example. The application then will not find the file anymore. You can either ensure that the image is being copied when the application is compiled (that alone may not work actually as unqualified URIs make the parser look for resources as far as i know, the designer however does not seem to care) or you can embed the image as a resource. I do not know how to change compile settings in Blend however as i only use Visual Studio...
I had a similar problem showing images (used Uri strings) in a listview in a Silverlight application. They appeared properly in Expression Blend, but were not visible when I started it from Visual studio.
I kept my images in a folder called Images in my project, by setting the "Build action" for my image properties into "Content" fixed the problem. Now they show up when running the application. Hope it helps!

Resources