从另一个项目添加用户控件时,VS Designer无法正常工作

时间:2016-08-01 17:05:53

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

我正在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>

在我的app项目中,引用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>

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

enter image description here

错误列表显示:

  

名称&#34; MyControl&#34;在命名空间中不存在   &#34;使用:MyProj.UIComponents&#34;

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

尝试使用clr-namespace

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

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

Bu不起作用:

  

未定义的CLR命名空间。 &#39; clr-namespace&#39; URI指的是命名空间   &#39; MyProj.UIComponents&#39;无法找到。

1 个答案:

答案 0 :(得分:0)

  

错误列表显示:

     

名称&#34; MyControl&#34;命名空间中不存在&#34;使用:MyProj.UIComponents&#34;。

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

引起的
  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. 命名空间错误,这似乎不是主要原因。

相关问题