0

要动态加载的示例 XAML

<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>

尽管 SuperButton 是在同一个程序集中定义的,但加载此失败并显示“无法加载未知类型的用户控件:超级按钮”。

我猜这是因为 SuperButton 有关联的代码隐藏?有没有办法帮助 XamlReader.Load() 找到它需要的东西?

4

1 回答 1

2

你正在做的应该工作 - 尝试在xmlns:usercontrols=''.

不久前我做了这件事(也许我写的 netGooey 库可能对你有用)。netGooey 将 XAML 动态加载到页面中,并支持用户定义的控件。

我的 XAML 标头如下所示:

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

控制使用,如:

<playback:volumeSlider Maximum="100" Minimum="0" Margin="42,180,62,0" Height="30" VerticalAlignment="Top" TickFrequency="10" TickPlacement="BottomRight" />

动态 XAML 加载如下:

            _gSystem.invokeOnLocalThread((Action)(() =>
            {
                FileStream fileStream = File.OpenRead(_sUIFile);
                DependencyObject dependencyObject = XamlReader.Load(fileStream) as DependencyObject;
                fileStream.Close();

                if (dependencyObject == null)
                    return;

                Content = dependencyObject;
            }), true);`

也许我忘记了让 XAML 注意到自定义控件的一些关键部分,但我很确定它最终会正常工作。

祝你好运。(希望完全限定的更改为您修复它)

http://inlay.codeplex.com/SourceControl/changeset/view/42822#549758

于 2012-07-09T19:52:29.657 回答