gpt4 book ai didi

c# - 平台特定项目的 XAML 中未正确引用 Windows 通用应用程序共享库代码

转载 作者:行者123 更新时间:2023-11-30 16:56:30 26 4
gpt4 key购买 nike

我在使用通用 Windows 应用程序和在设计 View 和 Blend 中查看 UserControl 时遇到问题。

我在共享库中有一个 BaseUserControl 和一些 ValueConverter。我在 App.xaml 中引用值转换器,BaseUserControl 是我想使用 Blend 帮助设计的特定 UserControl 的基础。

我目前在 Visual Studio XAML 设计器和 Blend 中遇到设计时错误:

The name "BaseUserControl" does not exist in the namespace "using:MyProject.Controls".

设计器 View 和 Blend 显示一个带有消息的框:

Invalid Markup - Check the Error List for more information

我可以构建项目并运行它,一切都显示出来并且工作正常。

如何让共享库代码在 Designer/XAML View and Blend 中被识别?

UserControl 的定义如下:

<controls:BaseUserControl
x:Class="MyProject.Controls.Menu.LeftNavigation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProject.Controls.Menu"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:MyProject.Controls"
mc:Ignorable="d"
Background="#232323"
d:DesignHeight="800"
d:DesignWidth="110"
>
...
</controls:BaseUserControl>

背后的代码很简单:

namespace MyProject.Controls.Menu
{
public sealed partial class LeftNavigation : BaseUserControl
{
public LeftNavigation()
{
this.InitializeComponent();
}
}
}

最佳答案

我注意到您使用了 controls:BaseUserControl,它扩展为 MyProject.Controls.BaseUserControl。但是,在您的代码隐藏 中,您已将 namespace 设置为MyProject.Controls.Menu。这将阻止设计者找到您的控件。

将控件标记更改为:

xmlns:controls="using:MyProject.Controls.Menu"

或者,将后面代码中的命名空间更改为 MyProject.Controls,然后您可以单独保留 xmlns:controls 标记。

另外,我发现设计者没有注意到在 Visual Studio 2013 中开发的通用应用(现在称为 Windows 应用)中共享库中的新控件 即使 namespace 正确。如果在您将 xaml 标记添加到页面时共享库已过时,就会发生这种情况。如果发生这种情况,请暂时删除您在 xaml 中包含 controls:BaseUserControl 的所有尝试。

然后,清理解决方案并重建它。这将允许共享库正确重建,将新标签暴露给设计者。最后,在构建成功后重新添加你的标签并重新构建。

希望对大家有所帮助。我今天刚刚为这个问题苦苦挣扎。

关于c# - 平台特定项目的 XAML 中未正确引用 Windows 通用应用程序共享库代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27949598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com