سؤال

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.

I am using Visual Studio 2013 on Windows 8.1

converter .h file:

#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);
};
}

converter .cpp file:

#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();
}

the xaml code:

<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>

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

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

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.

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 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.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top