如何将资源添加到具有 ResourceDictionary 的 class

How do I add resources to a class which has ResourceDictionary

当我在我的代码中添加:<s:RsuSensorVisible x:Key="RsuSensorLocalVisible"/> 时出现问题(x:需要关键属性)。代码贴在下面,还附上了一张图片。他们似乎相互抵消了。如果我删除 <s:RsuSensorVisible x:Key="RsuSensorLocalVisible"/> 完美。一些解决方案?

public class RsuSensorVisible : IValueConverter
                {
                    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                    {
                        //string stringValue = ((ContentControl)value).Content;
                        var stringValue = value as string;

                        if (!string.IsNullOrEmpty(stringValue))
                        {
                            return stringValue == "RSU" ? Visibility.Visible : Visibility.Hidden;
                        }
                        return Visibility.Visible;
                    }

                    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                    {
                        throw new NotImplementedException();
                    }
                }

    <Window.Resources>
           <s:RsuSensorVisible x:Key="RsuSensorLocalVisible"/> 
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Resources/DesignerItem.xaml" />
                    <ResourceDictionary Source="Resources/ToolboxItem.xaml" />
                    <ResourceDictionary Source="Resources/Toolbox.xaml" />
                    <ResourceDictionary Source="Resources/Connection.xaml" />
                    <ResourceDictionary Source="Resources/Stencils/FlowChartStencils.xaml" />
                    <ResourceDictionary Source="Resources/Stencils/ShapeStencils.xaml" />
                    <ResourceDictionary Source="Resources/Stencils/SymbolStencils.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>

只需将 RsuSensorVisible 变量放入 ResourceDictionary 声明中,问题就会消失。 Window.Resources 元素的内容必须是 ResourceDictionary。

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/DesignerItem.xaml" />
                <ResourceDictionary Source="Resources/ToolboxItem.xaml" />
                <ResourceDictionary Source="Resources/Toolbox.xaml" />
                <ResourceDictionary Source="Resources/Connection.xaml" />
                <ResourceDictionary Source="Resources/Stencils/FlowChartStencils.xaml" />
                <ResourceDictionary Source="Resources/Stencils/ShapeStencils.xaml" />
                <ResourceDictionary Source="Resources/Stencils/SymbolStencils.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <s:RsuSensorVisible x:Key="RsuSensorLocalVisible"/> 
        </ResourceDictionary>
    </Window.Resources>

我不能发表评论,所以我会post回答。

您尝试过将 <s:RsuSensorVisible x:Key="RsuSensorLocalVisible"/> 放入 ResourceDictionary 中吗?或者,尝试将 x:Key 属性赋予 ResourceDictionary。好像就是这个问题