xaml无效标记,但仍可编译/运行 [英] xaml Invalid Markup but still compiles/runs

查看:670
本文介绍了xaml无效标记,但仍可编译/运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加一个自定义转换器(布尔值到可见性).转换器的代码就可以了.看来地图还可以.但是,当我尝试将其添加为用户控件的资源时,我得到一个无效的标记,上面写着找不到BooleanToVisibilityConverter.请确认您没有丢失程序集,并且所有引用的程序集均已构建".即使显示了无效标记,项目仍可编译并运行,没有错误或崩溃.

I am trying to add a custom converter (Boolean to Visibility). The code for the converter is just fine. It seems to map ok. However, when I try and add it as a resource for a User Control I get an Invalid Markup which says "BooleanToVisibilityConverter was not found. Verify you are not missing an assembly and that all referenced assemblies have been built". Even with this Invalid Markup showing the project compiles and runs with no errors or crashes.

我正在Windows 8.1上使用Visual Studio 2013

I am using Visual Studio 2013 on Windows 8.1

转换器.h文件:

#pragma once

using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Interop;

namespace BooleanConverter{
public ref class BooleanToVisibilityConverter sealed : IValueConverter
{
public:
    virtual Platform::Object^ Convert(
        Platform::Object^ value,
        Windows::UI::Xaml::Interop::TypeName targetType,
        Platform::Object^ parameter,
        Platform::String^ language);

    virtual Platform::Object^ ConvertBack(
        Platform::Object^ value,
        Windows::UI::Xaml::Interop::TypeName targetType,
        Platform::Object^ parameter,
        Platform::String^ language);
};
}

转换器.cpp文件:

#include "pch.h"
#include "BooleanToVisibilityConverter.h"


using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Interop;
using namespace Windows::UI::Xaml::Data;

Object^ BooleanConverter::BooleanToVisibilityConverter::Convert(Object^ value, TypeName targetType, Object^ parameter, String^ language)
{
auto boxedBool = dynamic_cast<Box<bool>^>(value);
auto boolValue = (boxedBool != nullptr && boxedBool->Value);
return (boolValue ? Visibility::Visible : Visibility::Collapsed);
}

Object^ BooleanConverter::BooleanToVisibilityConverter::ConvertBack(Object^ value, TypeName targetType, Object^ parameter, String^ language)
{
throw ref new Platform::NotImplementedException();
}

xaml代码:

<UserControl
x:Class="SimpleShop.JobItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SimpleShop"
xmlns:converters="using:BooleanConverter"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Width="1030" Height="Auto" Background="Black">
<UserControl.Resources>
    <ResourceDictionary>
        <converters:BooleanToVisibilityConverter x:Key="BooleanToCollapesdConverter"/>
    </ResourceDictionary>
</UserControl.Resources>

我尝试将xmlns语句更改为: xmlns:converters ="clr-namespace:BooleanConverter"

I've tried changing the xmlns statement to: xmlns:converters="clr-namespace:BooleanConverter"

但这会引发错误,提示找不到BooleanConverter命名空间

but that throws up errors saying the BooleanConverter namespace can not be found

真正的奇怪之处在于,如果删除xmlns语句并重新输入,则intellisense表示找不到命名空间.但是,如果我只是简单地复制并粘贴该行本身,那么xmlns语句中的错误就会消失.

The really odd part is that if I delete the xmlns statement and re-type it in, intellisense says that the namespace can not be found. However if I simply copy and paste that line back over its self the error on the xmlns statement goes away.

我如何摆脱这个无效标记,因为它可以编译并运行,所以它似乎根本不是无效的.还是在实现此转换器时错过了一些东西?我正在使用MSDN作为参考(

How do I get rid of this Invalid Markup which doesn't seem to be invalid at all since it compiles and runs. Or have I missed something in implementing this converter? I am using MSDN as a reference (http://msdn.microsoft.com/en-us/library/ms747086(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1) and, to me, it doesn't look like I've implemented this wrong.

如果需要提供更多代码,请告诉我,但我相信这是与之相关的一切.

Please let me know if I need to supply more code but I believe this is everything that is relevant.

推荐答案

WinRT名称空间查找规则要求winmd发布的所有名称空间都存在于具有相同名称(或根名称空间名称)的winmd中.也就是说,名为"MyNamespace.MySubNamespace.MyClass"的类必须存在于MyNamespace.winmd或MyNamespace.MySubNamespace.winmd中,否则所有类型的加载程序都无法可靠地找到它.

WinRT namespace lookup rules require that all namespaces published by a winmd live in a winmd with the same name (or a root namespace name). That is, a class named "MyNamespace.MySubNamespace.MyClass" must live in either MyNamespace.winmd or MyNamespace.MySubNamespace.winmd, otherwise it cannot be reliably found by all type loaders.

我认为您的问题是名称空间是:: Converters,但布尔型转换器类可能隐藏在SimpleShop.winmd中.我会尝试将名称空间(和引用)更改为SimpleShop :: Converters,看看是否可以解决您的问题.

I think your issue is that your namespace is ::Converters, but your boolean converter class is probably hiding inside SimpleShop.winmd. I would try changing the namespace (and references) to SimpleShop::Converters and see if this resolves your issue.

这篇关于xaml无效标记,但仍可编译/运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆