c# - 为什么这段代码在编译正常时会引发 'does not exist in namespace' 错误?

标签 c# xaml windows-10 win-universal-app uwp

我正在开发适用于 Windows 10 的基于 XAML 的应用程序。在尝试在 Setters 中实现绑定(bind)时遇到问题,按照 this answer 。这是我的代码:

namespace Sirloin.Helpers
{
    internal class Binding
    {
        public static readonly DependencyProperty ContentProperty = // ...

        public static string GetContent(DependencyObject o) => // ...
        public static void SetContent(DependencyObject o, string value) => // ...
    }
}

这是我的 XAML:

<Page
    x:Class="Sirloin.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Sirloin"
    xmlns:h="using:Sirloin.Helpers"> <!--Some designer stuff omitted for clarity-->

    <Page.Resources>
        <ResourceDictionary>
            <Style x:Key="MenuButtonStyle" TargetType="Button">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="h:Binding.Content" Value="Symbol"/> <!--The XAML parser chokes on this line-->
                <Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
            </Style>
        </ResourceDictionary>
    </Page.Resources>
</Page>

出于某种原因,VS 设计器在到达具有特殊绑定(bind)语法的行时似乎会抛出错误;即,将 h:Binding.Content 设置为 Symbol。以下是该消息的屏幕截图:

但奇怪的是,代码似乎编译得很好。当我在 Visual Studio 中按 Ctrl + B 时,它会正常构建并成功输出二进制文件。当然,这样做的缺点是我无法使用设计器,它在每次构建项目时都会声明 XAML 为“无效标记”。

有人可以推荐我解决这个问题的方法吗?我尝试了建议here重新启动 Visual Studio 并删除缓存的二进制文件,但它似乎不起作用。

谢谢!

最佳答案

事实证明 Uchendu 是对的:我的问题之一是该类被标记为 internal 而不是 public。我还必须将其标记为sealed,并将依赖属性从字段更改为属性。例如,这个:

public static readonly DependencyProperty BlahProperty = /* ... */;

必须重构为:

public static DependencyProperty BlahProperty { get; } = /* ... */;

WinRT components don't allow exposing public fields .

经过这些修改后,我能够成功构建该库。

关于c# - 为什么这段代码在编译正常时会引发 'does not exist in namespace' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35097323/

相关文章:

c# - WPF 从 C# 代码设置文本框边框颜色

node.js - “NODE_ENV” 不被识别为内部或外部命令、可操作命令或批处理文件

java - 无法验证 ANT 1.9.7 设置

c# - 使用 C# 发送具有特定字节数的 ping

c# - WPF DataBinding 未使用 INotifyPropertyChanged 进行更新

.net - 设置其他文件的路径时,如何引用 WPF XAML 中的根项目目录?

multithreading - 关于 Keras fit_generator() 中的多处理和工作人员与 spyder 中的 Windows 10 的混淆

c# - 如何以编程方式清除缓存?

c# T 的实例

c# - 动态增加数组中元素的数量