简体   繁体   中英

Can't refer to a custom user control

I have a custom user control and I want to include it in the main page.
ColorPalette.xaml.h (It also includes xaml and xaml.cpp files, just I didn't post them)

namespace Colibry
{
    namespace Views {
        [Windows::Foundation::Metadata::WebHostHidden]
        public ref class ColorPalette sealed
        {
        public:
            ColorPalette();
        };
    }
}

MainPage.xaml

<Page
    x:Class="Colibry.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Colibry"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:cc="clr-namespace:Colibry.Views;assembly:Colibry"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <cc:ColorPalette></cc:ColorPalette>
    </Grid>
</Page>

But xmlns:cc="clr-namespace:Colibry.Views;assembly:Colibry" outputs an error:

Универсальный код ресурса (URI) "clr-namespace:Colibry.Views;assembly:Colibry" не является допустимым идентификатором пространства имен. Colibry E:\\Projects\\Colibry\\Colibry\\MainPage.xaml

For Englishman:

URI "clr-namespace:Colibry.Views;assembly:Colibry" is not available identifier of the name space. Colibry E:\\Projects\\Colibry\\Colibry\\MainPage.xaml

How to fix it? Just I learn C++/XAML only.

WPF/Silverlight/Windows Phone 8 way:

xmlns:controls="clr-namespace:MyApp.Controls"

WinRT/Universal App way:

xmlns:controls="using:MyApp.Controls"

Using one platform's syntax will fail to compile on the other and vice versa. There are small difference between "clr-namespace and using".This is discussed in greater detail in these two articles. XAML Namespaces and Namespace Mapping for WPF XAML and XAML namespaces and namespace mapping .

Above all,you could change "clr-namespace" to "using" and delete any assembly token and semi-colon (the assembly will be inferred). The result looks like this:

  xmlns:cc="using:RefCustomControlTest"

I have used the custom usercontrol like the following code and it working pretty well.

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  <cc:ColorPalette x:Name="test" />
</Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM