我正在开发适用于 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/