名称空间

时间:2015-05-07 10:12:08

标签: c# wpf

我正在为ToggleButton的标题创建一个自定义Expander,并附加一个按钮来删除扩展器的元素(我有一个扩展器列表视图)。

我得到的错误是:

"The name 'CustomToggleButton' doesn't exists in namespace 'clr-namespace:BoxEngine.BoxPreviewControls;assembly=BoxPreviewControls'"

这是theme.xaml文件的标题

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:modules="clr-namespace:BoxEngine.BoxPreviewControls;assembly=BoxPreviewControls">

这些是我的主题文件中生成错误的WPF行:

<ControlTemplate x:Key="ExpanderToggleButton"
                 TargetType="{x:Type modules:CustomToggleButton}">
    ...
</ControlTemplate>
...

<Style x:Key="Expander.ctlPrintData" TargetType="{x:Type Expander}">
...
                            <modules:CustomToggleButton
                                x:Name="ToggleButton"
                                Grid.Column="1"
                                IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,
                                                    RelativeSource={RelativeSource TemplatedParent}}"
                                OverridesDefaultStyle="True"
                                Template="{StaticResource ExpanderToggleButton}"
                                Background="Transparent" />
...
</Style>

最后这是CustomToggleButton类:

namespace BoxEngine.BoxPreviewControls
{
    public partial class CustomToggleButton : System.Windows.Controls.Primitives.ToggleButton
    {
        private Button delete_button_;

        // get the button objects as the templete is applied and add click event handlers
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            delete_button_ = GetTemplateChild("btnRemovePrint") as Button;
            delete_button_.Click += toggleBtnRemovePrint_Click;
        }

        // event exposed to container
        public static readonly RoutedEvent OnToggleBtnRemovePrint_ClickedEvent =
            EventManager.RegisterRoutedEvent("OnToggleBtnRemovePrint_Click", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(CustomExpander));

        // expose and raise 'OnBtnRemovePrint_ClickedEvent' event
        public event RoutedEventHandler OnToggleBtnRemovePrint_Click
        {
            add { AddHandler(OnToggleBtnRemovePrint_ClickedEvent, value); }
            remove { RemoveHandler(OnToggleBtnRemovePrint_ClickedEvent, value); }
        }

        private void toggleBtnRemovePrint_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(OnToggleBtnRemovePrint_ClickedEvent));
        }
    }

...
} //namespace

我不知道它是否重要,但是类和主题是在BoxPreviewControls项目中定义的,它是一个WPF控件库。 WPF应用程序在另一个项目中。

我看到了很多类似的问题,但我还没有找到解决方案,所以感谢任何帮助!

修改

我关闭了visual studio中的所有文件,重新启动它并重建了解决方案。 现在我收到一个新错误(最后一个错误,而不是代替它):

"Cannot find the public type 'CustomToggleButton' from the type reference"

编辑2

我删除了BoxPreviewControls的XAML标题中的程序集,现在它可以工作了。你知道为什么吗?

现在标题是:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:modules="clr-namespace:BoxEngine.BoxPreviewControls">

是因为我以这种方式在代码中加载主题吗?

ResourceDictionary resDict = (ResourceDictionary)Application.LoadComponent(new Uri("/BoxPreviewControls;component/Themes/theme.xaml", UriKind.Relative));

1 个答案:

答案 0 :(得分:0)

我删除了BoxPreviewControls的XAML标题中的程序集,现在它可以正常工作。

有关详细信息,请参阅编辑2