为什么这段代码编译正常时会引发 'does not exist in namespace' 错误?
Why does this code raise a 'does not exist in namespace' error when it compiles fine?
我正在为 Windows 10 开发基于 XAML 的应用程序。根据 。这是我的代码:
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 设计器在到达具有特殊绑定语法的行时似乎会抛出错误;即,将 h:Binding.Content
设置为 Symbol
的那个。这是消息的屏幕截图:
奇怪的是,代码似乎编译正常。当我在 Visual Studio 中按 Ctrl + B 时,它没有错误地构建并成功输出了二进制文件。当然,这样做的缺点是我不能使用设计器,每次我构建项目时它都声称 XAML 是 'Invalid Markup'。
有人可以向我推荐这个问题的解决方案吗?我尝试了 here 重新启动 Visual Studio 并删除缓存的二进制文件的建议,但它似乎不起作用。
谢谢!
事实证明 Uchendu 是对的:我的问题之一是 class 被标记为 internal
而不是 public
。我还必须将其标记为 sealed
,并将依赖属性从字段更改为属性。例如,这个:
public static readonly DependencyProperty BlahProperty = /* ... */;
必须重构为:
public static DependencyProperty BlahProperty { get; } = /* ... */;
因为 WinRT components don't allow exposing public fields.
经过这些修改后,我能够成功构建库。
在开始编译之前清除 Visual Studio 编辑面板,您将不会看到这些烦人的错误消息。也就是说,"X" 关闭所有当前的编辑会话,当然对所有 "Save changes to the following items" 消息回答是。
我正在为 Windows 10 开发基于 XAML 的应用程序。根据
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 设计器在到达具有特殊绑定语法的行时似乎会抛出错误;即,将 h:Binding.Content
设置为 Symbol
的那个。这是消息的屏幕截图:
奇怪的是,代码似乎编译正常。当我在 Visual Studio 中按 Ctrl + B 时,它没有错误地构建并成功输出了二进制文件。当然,这样做的缺点是我不能使用设计器,每次我构建项目时它都声称 XAML 是 'Invalid Markup'。
有人可以向我推荐这个问题的解决方案吗?我尝试了 here 重新启动 Visual Studio 并删除缓存的二进制文件的建议,但它似乎不起作用。
谢谢!
事实证明 Uchendu 是对的:我的问题之一是 class 被标记为 internal
而不是 public
。我还必须将其标记为 sealed
,并将依赖属性从字段更改为属性。例如,这个:
public static readonly DependencyProperty BlahProperty = /* ... */;
必须重构为:
public static DependencyProperty BlahProperty { get; } = /* ... */;
因为 WinRT components don't allow exposing public fields.
经过这些修改后,我能够成功构建库。
在开始编译之前清除 Visual Studio 编辑面板,您将不会看到这些烦人的错误消息。也就是说,"X" 关闭所有当前的编辑会话,当然对所有 "Save changes to the following items" 消息回答是。