类型引用找不到名为...不是因为缺少程序集名称

本文关键字:程序集 是因为 引用 找不到 类型 | 更新日期: 2023-09-27 18:35:57

只是为了提前弄清楚这一点,我很清楚关于此错误的大量问题和答案是由于声明中缺少程序集名称而导致的,这里的情况并非如此。

在过去的几天里,我一直看到一些非常严重的不稳定,代码本身似乎会停止编译不真实的抛出错误,然后在关闭然后重新打开 VS 后神奇地再次开始工作,也让我的自定义控件停止出现在设计器中并在 ctor() 中吐出幻影错误,然后修复自己,我的控件中的依赖项属性从 VS 属性资源管理器中丢失,但仍然可从 XAML 访问。我想知道我是否可能在 VS 中出现某种错误,当我发现 VS 中的错误导致我的 g.cs 文件损坏时,它就发生了......

严重性代码说明项目文件行抑制状态 错误类型引用找不到名为 '{clr-namespace:ODIF;assembly=PluginInterface}Global'. CustomControls_WinX86 xxxxx''CustomControls_WinX86''ChannelBoxMenu.xaml 17

为我的用户控件完成 XAML:

UserControl x:Name="ChannelBoxMenuControl" x:Class="CustomControls_WinX86.ChannelBoxMenu"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CustomControls_WinX86"
             xmlns:ODIF="clr-namespace:ODIF;assembly=PluginInterface"
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="250">
    <Grid>
        <Menu x:Name="menu">
            <MenuItem x:Name="menuItem" BorderThickness="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Template="{DynamicResource MarginlessMenuItem}" Width="{Binding ActualWidth, ElementName=menu, Mode=OneWay}" Height="{Binding ActualHeight, ElementName=menu, Mode=OneWay}" >
                <MenuItem.Header>
                    <local:ChannelBox Width="{Binding ActualWidth, ElementName=menuItem, Mode=OneWay}" Height="{Binding ActualHeight, ElementName=menuItem, Mode=OneWay}" Channel="{Binding SelectedChannel, ElementName=ChannelBoxMenuControl}"/>
                </MenuItem.Header>
                <MenuItem.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Source={x:Static  ODIF:Global.ConnectedDevices}, Mode=OneWay}"><!--THIS IS WHERE THE ERROR IS THROWN-->
                        <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                            <Image Source="{Binding StatusIcon}" Width="16" Height="16">
                                <Image.Style>
                                    <Style TargetType="{x:Type Image}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Icon}" Value="{x:Null}">
                                                <Setter Property="Visibility" Value="Collapsed" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Image.Style>
                            </Image>
                            <TextBlock Text="{Binding DeviceName}"/>
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </MenuItem.ItemTemplate>
            </MenuItem>
        </Menu>
    </Grid>
</UserControl>

以及我引用的程序集中的相关代码:

namespace ODIF
{
    public static class Global
    {
        internal static GlobalStore Store = new GlobalStore();
        public static AsyncObservableCollection<InputDevice> ConnectedDevices
        {
            get
            {
                return Store.inputDevices;
            }
        }
    }
    internal class GlobalStore
    {
        internal AsyncObservableCollection<InputDevice> inputDevices;
    }
}

另外值得注意的是,当我开始输入HierarchicalDataTemplate ItemsSource的路径时,智能感知会很好地拾取路径ODIF:Global.ConnectedDevices并自动完成它,但随后抛出它找不到它的错误。

类型引用找不到名为...不是因为缺少程序集名称

并不是真正解释为什么上述方法不起作用,据我所知,它应该这样做。但是,如果有人遇到相同的问题,我的解决方法是在UserControl的类中创建一个静态属性,该属性引用其他程序集中的静态属性:

添加到通道框菜单:用户控件

    public static AsyncObservableCollection<ODIF.InputDevice> ConnectedDevices
    {
        get
        {
            return Global.ConnectedDevices;
        }
    }

并将我的装订修改为:

ItemsSource="{Binding ConnectedDevices, ElementName=ChannelBoxMenuControl}

它不像直接引用那样干净,但它具有工作的好处。