Figuring out Silverlight namespaces and assemblies - silverlight

Often when you find examples of Silverlight code on the web it may only contain a snippet of code rather than the full set needed to make it work. This causes me immense frustration when I am trying to work out what namespace and/or assembly declaration to use at the top of the xaml file.
For example, take the following snippet (which shows how to add a list of items as a static resource)
<UserControl.Resources>
<controls:ObjectCollection x:Key="SampleData">
<sys:String>User 1</sys:String>
<sys:String>User 2</sys:String>
<sys:String>User 3</sys:String>
</controls:ObjectCollection>
</UserControl.Resources>
How is it possible for me to determine what namespace and assembly this guy used as the "controls" alias???

What I usually do is:
- Try to add the element to my Xaml, Resharper will help me here if the class is within a referenced assembly.
- Search the documentation for the class name. Silverlight sdk doc and then the Toolkit doc.
In this case, in SL3, the ObjectCollection class is within the Toolkit:
Namespace: System.Windows.Controls
Assembly: System.Windows.Controls.Toolkit (in System.Windows.Controls.Toolkit.dll)

Related

Localizing strings in WPF codebehind using XAML ResourceDictionary

I'm localizing a WPF app using the LocBaml method. Everything works great for UI defined in .xaml files. However I have a few strings that are generated in codebehind that must also be localized. So I tried to take the approach recommended by Microsoft in this article. I have a xaml resource dictionary file as such:
<ResourceDictionary x:Uid="ResourceDictionary_1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<!-- String resource that can be localized -->
<system:String x:Uid="system:String_1" x:Key="localizedMessage">en-US Message</system:String>
</ResourceDictionary>
I then use a third-party tool to generate the localized resources.dll containing a spanish version of the resource dictionary.
However, when I call
string localizedMessage = (string)Application.Current.Resources["localizedMessage"];
localizedMessage always returns the value defined in the en-US version of the dll. I must be misunderstanding something. What do I have to do to get the localized version of the string returned?
After discussion with the OP, the problem is in setting the language too late (in App's OnStartup).
Indeed, the localized resource dictionaries are loaded once, using the current language of the thread. If the thread language changes too late, the resources are already loaded, and no reload is triggered.
A solution in this particular case would be to change the UI thread's locale as early as possible -- that is, in the App's constructor.

Problem reference converter from an external assembly

I am creating a Resource Dictionary, where I reference all my converters, so there is no need to reference each individual converter.
My converters are in the different assembly, to import them I do the following:
Add reference to external assembly
Create a Resource Dictionary
Add xml namespace referencing Converters assembly
Reference converters
So my Dictionary looks like:
<ResourceDictionary xmlns:Converters="clr-namespace:Client.Utilities.Converters;assembly=Client.Utilities">
<Converters:BoolToBrushConverter x:Key="boolToBrush"/>
</ResourceDictionary>
However I get the following exception when trying to build:
Error 18 The tag 'BoolToBrushConverter' does not exist in XML namespace 'clr-namespace:Client.Utilities.Converters;assembly=.Client.Utilities'. Line 12 Position 6. C:\Resources.Tests\Resources\ResourceDictionaries\Converters\ConvertersResources.xaml 12 6 Client.eZenith.Resources.Tests
Any ideas why that is happening?
Note: From intellisense it seems that namespace for Converters assembly is correct, as all converters show up in the suggestion list after typing <Converter:
Edit: VS and blend designer both are able to find that converter, when rendering control preview.
Edit: I have figured out, that it is nothing to do with dictionaries being merged. The same issue appears, when adding a converter to Window's Resources.
I have found the problem eventually, it is merged resourcedictionary bug introduce in .NET 4.
I have fixed it by adding an empty style into the merged resource dictionary (previously I had a RD where I was merging other RD and nothing else).
here is a blog post which I found recently which describes the same problem.
Try using
<ResourceDictionary xmlns:Converters="clr-namespace:Client.Utilities.Converters;assembly=Client.Utilities">
instead.
Change: clr-namespace instead of namespace.

WPF UserControl cannot find XAML resource in referencing project

In my WPF project i keep a user control in a separate library project. The user control accesses resources in a separate XAML file, like this:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/ViewResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Local styles here -->
</ResourceDictionary>
</UserControl.Resources>
The resource file, ViewResources.xaml, resides in a folder in the control library project named Resources. It has the default build action (Page) and custom tool (MSBuild:Compile).
The problem is when I reference the control library in my WPF application and use the user control. At runtime, I get the following XamlParseException:
Set property 'System.Windows.ResourceDictionary.Source' threw an exception.
...which wraps the IOException:
Cannot locate resource 'resources/viewresources.xaml'.
How can I fix this? I have tried to change the resource file's build action to "content" and have it copied to the output directory (that works for files and similar "dumb" resources). But to no avail. Also, it doesn't work property in the user control then.
Is there a better way to specify the path?
Will I have to move the resource file to the application project (I'd rather not, as it belongs in the user control's domain).
Found it.
Turns out there is a better way to specify the path, Pack URIs. I changed the XAML to the following:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/RoutingManager;component/Resources/ViewResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Local styles here -->
</ResourceDictionary>
</UserControl.Resources>
and that fixed it.
I thought it was worth posting this just in case anyone else is struggling with the same problem, as I've spent over two hours fighting with syntax, etc. only to find that the solution was dead simple, but not that apparent:
When referencing a packed resource from another control library, it seems to work fine at design time, and even compiles without error, but fails at runtime with the 'Set property 'System.Windows.ResourceDictionary.Source' threw an exception' error. It turns out that simply referencing the resource assembly from your control library is not enough, you ALSO need to add a REFERENCE to the assembly containing the resource dictionary in you main application assembly, else it seems it does not get compiled into the application. (i.e. Startup Application (the one with app.xaml) -> Add Reference -> select assembly with referenced resource file/s).
Hope this helps!
In my case I had the ResourceDictionary and the UserControl on the same Library, but separate from the main application. What worked for me was specifying the name of the assembly in the format Adam suggested in the comment AND I had to change the ResourceDictionary in the project from Embedded Resource to Page. I didn't try using the pack:// format, but I assume it would work too.
<ResourceDictionary Source="/AssemblyName;component/Assets/MyResource.xaml"/>
I had the same error (IOException - file not found), which cost me a day of my life that I'll never get back.
Using neither the simpler "/assemblyname..." nor the "pack://...." syntax worked for me.
I was referencing the resource assembly in my main assembly correctly.
The error disappeared when I changed my xaml resource file Build Action property to "Resource", as mentioned above.
However, I then encountered a XamlParseException at this line:
<ImageBrush x:Key="WindowBackground" ImageSource="Images/gradient.png" />
(which I had hand-typed).
This left the xaml resource file I was trying to include with effectively an invalid dependency.
Oddly the fix was to delete the ImageSource property I had typed, re-insert it BUT select the image from the pulldown menus that appear as a result.
Even though the resulting line appears exactly the same, it clearly isn't.
Starting to dislike WPF (VS2013), but hope this helps.
:0/
I had the same situation, but the Pack URIs didn't help me, I was still getting "Cannot locate resource..." exception in the referencing (executable) project. What helped me, was the setting of my ResourceDictionary files in the custom control library project as Embedded Resource.

Can't use silverlight namespace

Whenever I try to reference the following namespace in my XAML, the code compiles and the project starts, but the InitializeComponent method throws an error. Here's the XAML reference:
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
and here's the use of ExtendedVisualStateManager
<ei:ExtendedVisualStateManager/>
The error is this:
The type 'ExtendedVisualStateManager' was not found because 'http://schemas.microsoft.com/expression/2010/interactions' is an unknown namespace. [Line: 19 Position: 37]
Is there a new namespace I need to use to use this control?
Here are some facts.
The Microsoft.Expression.Interactions.dll version 4.0.5.0 contains the namespace Microsoft.Expression.Interactivity.Core.
This Microsoft.Expression.Interactivity.Core contains the type ExtendedVisualStateManager.
The Microsoft.Expression.Interactions.dll version 4.0.5.0 carries a XmlnsDefinition that maps the URL "http://schemas.microsoft.com/expression/2010/interactions" to the namespace Microsoft.Expression.Interactivity.Core.
Hence a project referencing version 4.0.5.0 of Microsoft.Expression.Interactions.dll can contain Xaml using xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" that can then contain ei:ExtendedVisualStateManager.
You'll note I've repeated the version number a few times. If you do have an interactions dll referenced in a Silverlight 4 project but your code doesn't work then perhaps its the wrong version. However in that case Dan's answer should still have worked.
Make sure your Silverlight application has a reference to the Microsoft.Expression.Interactions assembly.
<UserControl
xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
...other namespaces... />
<VisualStateManager.CustomVisualStateManager>
<ei:ExtendedVisualStateManager/>
</VisualStateManager.CustomVisualStateManager>
</UserControl>
I had everything correct per the other answers and like you, the problem still existed. It was failing at runtime on a usercontrol in my project (and that project did reference Microsoft.Expression.Interactions).
However, that usercontrol was being used on a form in another project. Once I added the reference to Microsoft.Expression.Interactions to the outer project, the runtime error was solved. I was not loading assemblies dynamically and so I'm not 100% certain why this was a problem.
I think you should look in your project's properties. Find the references (Microsoft.Expression.Interactions or/and other "Expression" assemblies you may use, and set the "Copy Local" property to TRUE and try it again.
None of the answers solved this puzzling problem to me.
Apparently I needed Microsoft Expression Blend SDK for Silverlight 4.
Installing it has solved the issue.

Creating a HierarchicalDataTemplate in Silverlight with code

I'm trying to create a HierarchicalDataTemplate (from the Silverlight Toolkit) in code in Silverlight following this advice on creating DataTemplates from code:
Creating a Silverlight DataTemplate in code
However, I haven't been able to get it to work for HierarchicalDataTemplate.
I tried using XamlBuilder that ships with Silverlight Toolkit, but that gives me an error.
I've tried XamlReader, and have included various default and prefixed namespaces, but with no luck.
If I run XamlBuilder's Build method, I get the following string:
<HierarchicalDataTemplate xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:attached=\"clr-namespace:MyStuff;assembly=MyStuff\" xmlns=\"clr-namespace:System.Windows;assembly=System.Windows.Controls\"><attached:MyUserControl /></HierarchicalDataTemplate>
The error I get is:
AG_E_PARSER_NAMESPACE_NOT_SUPPORTED
With the Silverlight Xaml parser, the default xmlns must be "http://schemas.microsoft.com/winfx/2006/xaml/presentation", even if you don't use it. So add that xmlns declaration to your Xaml string, and change the System.Windows clr-namespace declaration to use some prefix.

Resources