Load catel view using ViewModelToViewConverter in xmlnsdefinition - wpf

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.

Related

How to declare Labels in xaml for use in both WPF and Silverlight

I am working on a UserControl in a Silverlight project in visual studio 2010.
This user control uses Labels as follows:
<sdk:Label .../>
-or-
<sdk:Label>
singleObject
</sdk:Label>
-or-
<sdk:Label>stringContent</sdk:Label>
as described here
However, when I create a WPF project link it to Silverlight Library Project:
(by adding existing items (Add Link) to cs classes and the .xaml file defining the control)
I get the following error in build:
The tag 'Label' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk'. Line 49 Position 18. myfile.xaml 49 18 MyLibraryWPF
This doc shows that labels in WPF are declared without the sdk:
like this:
<Label>
Content
</Label>
yet, I cannot change that in my Silverlight project. It will be a real waste to create a whole new project and duplicate the code to make it work.
How to make the Labels work with both Silverlight and WPF projects?
Here is a piece of Code in xaml that can be shared by WPF and Silverlight and looks like label with Background coloe:
<Border Margin="3,0,3,3" Background="#FF00B050">
<TextBlock Text="txt122" FontSize="13" FontWeight="SemiBold" Height="21" Name="label15" Width="133" ></TextBlock>
</Border>
This means that a Label cannot be used in a shared XAML.

Change WPF Source in Runtime

I would like to change the source attribute in runtime from the MainWindow.xaml.cs file. My current code is displayed under here.
<controls:StartVideoCallButton Name="accountString" Source="sip:stj#starck.onmicrosoft.com" Height="75" Width="314" Background="#FFC7C7C7"></controls:StartVideoCallButton>
This works for me:
accountString.Source = "sip:new.sip#company.com";
Or you could bind the source property using the MVVM pattern to separate your UI from your business logic - this is the preferred approach for WPF apps

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;

How can I instantiate WPF elements via their type when they have StaticResources?

I need to instantiate WPF types (say, a UserControl or a Page) via reflection for a designer. The problem I'm having is that when I attempt to instantiate these using Activator.CreateInstance I get a TargetInvocationException which wraps, in the end, an exception thrown by the StaticResource markup extension.
Clarification: The types are in a different assembly that is loaded at runtime!
Apparently, Activator.CreateInstance can't instantiate types that use the {StaticResource XXX} markup extension, even when the static resource is defined in the type's definition.
So, if all you have is the following type's definition (Type + xaml file):
<Page x:Class="Hurr.Durr">
<Page.Resources>
<ControlTemplate x:Key="whatever">
<TextBlock Text="This is a stupid example."/>
</ControlTemplate>
</Page.Resources>
<ContentControl Template="{StaticResource whatever}" />
</Page>
How would you spin this up at runtime?
BTW, requiring that you use DynamicResource instead is not acceptable. Does Visual Studio require this? Yet VS is able to spin up a copy of your WPF framework element and stick it in the designer.
Here's a sample application (beta 2, sorry) that demonstrates the issue.
http://cid-f8be9de57b85cc35.skydrive.live.com/self.aspx/Public/ResourcesGoKaboom.zip
The app attempts to load a Page with a drawing in its resource dictionary. I try to load it dynamically using Application.LoadComponent and Activator.CreateInstance and they both fail.
Okay, the example above works without an issue. The issue was that I was using a StaticResource within an element's attributes that referenced a resource defined within that element.
What was throwing me was that this works within visual studio. So in the designer everything looked okey dokey but IRL it was all pear shaped. IMHO, this is a bug and I've reported it as such:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=519244
Not sure if its a failure of VS to.... fail, or if the StaticResource extension was working in VS but not IRL.
The problem appears to be with the Page you're trying to load. Your Page looks like this (omitting a bunch of xmlns etc. plumbing):
<AwesomePage Title="AwesomePage" Background="{StaticResource awesomeface}">
<AwesomePage.Resources>
<DrawingBrush x:Key="awesomeface" Stretch="Uniform">
<!-- details snipped -->
</DrawingBrush>
</AwesomePage.Resources>
</AwesomePage>
This tries to use the awesomeface resource before it is defined. StaticResource does not allow this kind of forward referencing: therefore the StaticResourceExtension.ProvideValue fails and you get the error you have run into.
To fix this, put a Border immediately inside the Page, and move the Background setter from the Page to the Border. Then put all your content inside the Border.

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