【问题标题】:VS Designer not working when adding a user control from another project从另一个项目添加用户控件时 VS Designer 不工作
【发布时间】:2016-08-01 17:05:53
【问题描述】:

我正在 Visual Studio 2015 中创建一个通用应用程序。我的通用应用程序引用了一个名为 UIComponents 的通用库。

UIComponents我创建了一个用户控件:

namespace MyProj.UIComponents {
  public sealed partial class MyControl : UserControl
  {
    public MyControl()
    {
      this.InitializeComponent();
    }
  }
}

使用以下 xaml:

<UserControl
    x:Class="MyProj.UIComponents.MyControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyProj.UIComponents"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid>
        <Rectangle Fill="White" HorizontalAlignment="Left" Height="280" Stroke="Black" VerticalAlignment="Top" Width="380" Margin="10,10,0,0"/>
        <TextBox x:Name="textBox" Margin="20,20,20,20" TextWrapping="Wrap" Text="TextBox"/>

    </Grid>
</UserControl>

在引用 UIComponents 的应用项目中,我这样做:

<Page
    x:Class="MyProj.App.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyProj.App"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ui="using:MyProj.UIComponents"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ui:MyControl></ui:MyControl>
    </Grid>
</Page>

但是当我尝试让设计器显示我得到的页面时:

错误列表显示如下:

名称空间中不存在名称“MyControl” “使用:MyProj.UIComponents”。

有趣的是,整个解决方案构建得很好,但设计师没有合作。

尝试使用clr-namespace

在 WPF 中有类似的问题,因此不是严格的通用应用程序,它们在解决方案使用的答案上标记为已解决:

xmlns:ui="clr-namespace:MyProj.UIComponents" 

不起作用的Bu:

未定义的 CLR 命名空间。 'clr-namespace' URI 指的是命名空间 找不到“MyProj.UIComponents”。

【问题讨论】:

  • 移动一点,你挡住了我们对错误列表的看法。
  • 是的,抱歉忘记发布错误
  • 类名是否为“MyProj.UIComponents.MyControl.MyControl”值得怀疑。所以它肯定应该是“使用:”MyProj.UIComponents” ..
  • 我粘贴了错误的字符串抱歉,我已修复它
  • 解释投票,否则人们无法理解他们做错了什么......

标签: c# .net visual-studio win-universal-app windows-10-universal


【解决方案1】:

错误列表显示如下:

命名空间“using:MyProj.UIComponents”中不存在名称“MyControl”。

根据我的经验,这可能是由

引起的
  1. UIComponents 库没有建立。

    创建Library项目后,VS会在YourProject.proj文件中添加如下代码,VS设计器会检测到:

    <ItemGroup>//The following lines will be added.
       <Page Include="MyControl.xaml">
       <Generator>MSBuild:Compile</Generator>
          <SubType>Designer</SubType>
       </Page>
    </ItemGroup>
    

    所以,请尝试构建您的库项目并在您的主项目中重新引用它。重新加载后,设计器应该正确加载内容。

    注意:构建库时的架构(x64,x86)应该与当前架构相同。 (例如,当您使用 x86 构建库时。当您当前的架构是 x64 时,Designer 无法正确加载)。

  2. 命名空间错误,这里似乎不是主要原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多