Creating a HierarchicalDataTemplate in Silverlight with code - silverlight

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.

Related

Load catel view using ViewModelToViewConverter in xmlnsdefinition

Currently we have a WPF userControl Library where the namespaces are mapped to an XML Namespace using the
[assembly: XmlnsDefinition]
statement.
When we try to use controls out of this library in a WPF Application that references this assembly, using a ContentPresenter
<ContentPresenter Content="{Binding CurrentContent, Converter={StaticResource ViewModelToViewConverter}}" />
we always get a
System.Windows.Markup.XamlParseException("Type reference cannot find type named '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}XYZView")
What are we doing wrong - or is catel not designed to show controls using the ViewModelToViewConverter if they are in a XmlnsDefinition?
Okay, fixed - the error was on another place in the source... Catel definitely works also when using Xmlnsdefinitions.

how to declare ObservableCollecion in Silverlight Xaml

I want to add declarations for a Object with type of ObservableCollection in Silverlight Xaml. This is what I have done.
1.Add the xmlns declaration like this:
xmlns:col="clr-namespace:System.Collections.ObjectModel;assembly=System.Windows"
2.Add the ObservableCollection declaration like this:
<col:ObservableCollection x:TypeArguments="sys:String" x:Key="AnaBase">
<sys:String>string1</sys:String>
<sys:String>string2</sys:String>
<sys:String>string3</sys:String>
</col:ObservableCollection>
3.The above code works fine in Vs2010 editor because it doesn't report any errors.
The problem is, when I try to build such xaml it always outputs error such as
"The tag 'ObservableCollection' does not exist in XML namespace 'clr-namespace:System.Collections.ObjectModel;assembly=System.Windows'."
It seems to me that the namespace resolvers in editing and building of VS work in different ways. Could anyone please to explain this phenomenon to me? Thanks a lot.
Finally I found out the answer. The generics is not supported in Silverlight 4. That's why ObservableCollection can't be seen in Intellisense of XAML.
Answer: WPF: Xaml, create an observable collection<object> in xaml in .NET 4.0

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.

Figuring out Silverlight namespaces and assemblies

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)

XamlParseException using Silverlight Toolkit control in Expression Blend

I am having a strange issue opening up my UserControl in Expression Blend when using a Silverlight Toolkit control. My UserControl uses the toolkit's ListBoxDragDropTarget as follows:
<controlsToolkit:ListBoxDragDropTarget mswindows:DragDrop.AllowDrop="True" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<ListBox ItemsSource="{Binding MyItemControls}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<controlsToolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</controlsToolkit:ListBoxDragDropTarget>
Everything works as expected at runtime and looks fine in Visual Studio 2008. However, when I try to open my UserControl in Blend I get XamlParseException: [Line: 0 Position: 0] and I can not see anything in the design view. More specifically Blend complains:
The element "ListBoxDragDropTarget" could not be displayed because of a problem with System.Windows.Controls.ListBoxDragDropTarget: TargetType mismatch.
My silverlight application is referencing System.Windows.Controls.Toolkit from the Nov. 2009 toolkit release, and I've made sure to include these namespace declarations for the ListBoxDragDropTarget:
xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
xmlns:mswindows="clr-namespace:Microsoft.Windows;assembly=System.Windows.Controls.Toolkit"
If I comment out the ListBoxDragDropTarget control wrapper and just leave the ListBox I can see everything fine in the design view without errors. Furthermore, I realized this is happening with a variety of Silverlight Toolkit controls because if I comment out ListBoxDragDropTarget and replace it with
<controlsToolkit:BusyIndicator />
the same exact error occurs in Blend. What is even weirder is that if I start a brand new silverlight application in blend I can add these toolkit elements without any kind of error, so it seems like something dumb that is happening with my project references to the toolkit assemblies.
I'm pretty sure this has something to do with loading the default styles for the toolkit controls from its generic.xaml, since the error has to do with the TargetType and Blend is probably trying to load up the default styles.
Has anyone encountered this issue before or have any ideas as to what may be my problem?
Hi we had exactly the same issue, we solved it by checking the references in the project which was having this problem. All referenced toolkit assemblies should be in the same directory on disk.
Our project reference to the System.Windows.Controls.Toolkit.dll always 'jumped' back to the orginal path which was causing our issue. We solved by editing the project file in notepad++ (or any of your favorite text-editor) and hardcode the path where it could find the assembly.
Hope this helps.
For Visual Studio (maybe Blend too) you need to add a reference to :
System.Windows.Controls.Toolkit.Internals.dll
"C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Bin\System.Windows.Controls.Toolkit.Internals.dll"
I'm using Silverlight 5 toolkit and having XamlParseException when using BusyIndicator toolkit control in Expression Blend for SL 5, all above solutions didn't helped but I found another workaround, its rather dirty but allows me to make BusyIndecator work in Expression Blend,
derive from control
public class BusyIndicatorEx : BusyIndicator
{
public BusyIndicatorEx()
{
this.DefaultStyleKey = typeof(BusyIndicatorEx);
}
}
create style for derived control (just copy style from BusyIndicator source code to themes/generic.xaml, and change target type to local:BusyIndicatorEx)
I had the same problem which has been resolved by introducing a dummy reference in code behind to the Wrap Panel.
I know it is a little C++y but I can imagine it is because we have indirect reference to wrap panel inside a template and not on the top level page so the loader don't know what to load on initialization. I really like understand the exact reason, though.
I just referenced System.Windows.Controls.Toolkit and introduce following member in code behind:
System.Windows.Controls.WrapPanel _dummy;

Resources