Example XAML to be loaded dynamically
<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:usercontrols='clr-namespace:App.Views.UserControls'>
<TextBlock>Why don't you click the button?</TextBlock>
<usercontrols:SuperButton
Command="{Binding DataContext.OpenURLNew,RelativeSource=
{RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}"
CommandParameter="50">
ClickMe</usercontrols:SuperButton>
</Grid>
Loading this fails with ‘can not load unknown type usercontrols:superbutton’, despite the fact that SuperButton is defined in the same assembly.
I’m guessing this is because SuperButton has associated code-behind? Is there a way to help XamlReader.Load() find what it needs?
What you’re doing should work – try using the fully qualified assembly name in
xmlns:usercontrols=''.I did this exact thing a while back (perhaps the netGooey library I wrote might be useful for you). netGooey loads XAML dynamically into the page with support for user-defined controls.
My XAML header looked like this:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="499" Height="579"
xmlns:playback="clr-namespace:inlayShared.ui.controls.playback;assembly=inlayShared"
xmlns:library="clr-namespace:inlayShared.ui.controls.library;assembly=inlayShared">
Control use like:
<playback:volumeSlider Maximum="100" Minimum="0" Margin="42,180,62,0" Height="30" VerticalAlignment="Top" TickFrequency="10" TickPlacement="BottomRight" />And the dynamic XAML loading like this:
Perhaps I forgot some key part to getting XAML to notice the custom controls but I’m pretty sure it just ended up working.
Good luck. (Hopefully the fully qualified change fixes it for you)
http://inlay.codeplex.com/SourceControl/changeset/view/42822#549758