How can I access resources from external assembly? - wpf

I don't seem to be able to access remote resources from an external assembly.
The following line in MainWindow.xaml isn't working, no background color is getting displayed:
<StackPanel Grid.Row="0" Background="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:DummyForSharingResources}, ResourceId=TestBg}}"></StackPanel>
I published a corresponding repository to GitHub containing my Visual Studio 2019 solution. It contains all the details.
What I did so far
"Themes/generic.xaml" file created in supplemental assembly (StylesAndResources), filled with resources.
ThemeInfoAttribute updated in application assembly to reference ResourceDictionaryLocation.ExternalAssembly.
Dummy class created in StylesAndResources in order to be able to have a ComponentResourceKey be generated.
What am I doing wrong?

Related

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.

error when hosting Windows Form user control in WPF

Im trying to host Magtek card reader into WPF, so i encapsulated their demo into a Windows forms user control, when i use the dll from this control in a Windows application, it works fine, but when used in WPF it gives this error:
Could not load file or assembly 'Interop.ctlUSBHID, Version=1.3.0.0,
Culture=neutral, PublicKeyToken=null' or one of its dependencies. The
system cannot find the file specified.
My code:
xmlns:mcl="clr-namespace:CardReader;assembly=CardReader"
<WindowsFormsHost Name="wfh"
DockPanel.Dock="Top"
Height="300">
<mcl:Reader Name="_Reader"/>
</WindowsFormsHost>
Your help is much appreciated
Have you referred the assemblies,
WindowsFormsIntegration and
System.Windows.Forms
in your wpf application?
What worked for me was removing the Interop.ctlUSBHID file from project's References. This file is not supposed to be added manually in references as it will be generated automatically by ctlUSBHID.dll while code compilation.

Weird UserControl image path behaviour in Blend 4 and WPF 4.0

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");

WPF resources addin

I have a WPF application that runs as an add-in in another application.
(In this case it is COM based but I don't think it matters for the problem I have)
The application works fine when I run it standalone. When running as add-in there is a problem with resources.
First: (minor)
The "pack:" uri scheme is not available at all. I can work around this but it would be nice to understand why (and if it can be solved)
Second: (showstopper)
XAML resources can't be found (the XAML is found!). The errormessages says that Assembly.GetEntryAssembly() is null (which is understandable) and that I should set Application.ResourceAssembly.
The problem is that my application consists of 2 assemblies which both contain XAML and resources. Which one should I set as resource assembly? I have tried both and neither of them work. (The errormessage just says that the resource can't be found)
The resources that can't be found are normal text with an autogenerated static class. The XAML looks like:
<TextBox Text="{StaticResource my:Texts.ButtonText}">

Resources