使用xaml reader加载的外部xaml文件中的自定义命令

时间:2012-08-08 10:54:33

标签: xaml command xml-namespaces

我迷失了自定义命令和绑定。 我有一个主要的XAML文件,它只是一个容器。

<Window x:Class="MyNS.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         
        xmlns:a="clr-namespace:MyNS.Actions"
        Title="MainWindow" Height="350" Width="525">
    <Window.CommandBindings>
        <CommandBinding 
                    Command="{x:Static a:OpenWindow.Cmd}"                
                    CanExecute="CanExecuteActionAsCommand" 
                    Executed="ExecuteActionAsCommand" />
    </Window.CommandBindings>

    <Grid Name="ContentPanel">

    </Grid>
</Window>

现在我想在运行时加载一个带有XAML Reader的附加XAML文件。总而言之,它运作良好。但是现在我有一个带有自定义命令的按钮,我迷失了。 附加文件如下所示:

<StackPanel             
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:a="clr-namespace:MyNS.Actions">

      <Button Content="Show Properties" Grid.Row="1" 
              Command="{x:Static a:OpenWindow.Cmd}" 
              CommandParameter="startup_screen2" />
</StackPanel>

现在的问题是XAML Reader无法解决“x:Static”。

我的问题是,如果有人知道如何定义我可以在我的附加XAML文件中调用它的自定义命令。

非常感谢。

1 个答案:

答案 0 :(得分:0)

尝试将程序集名称添加到xmlns的名称空间声明中:a在松散的XAML中:

xmlns:a="clr-namespace:MyNS.Actions";assembly=YourAssemblyName

现在XamlReader会找到你的自定义命令。