xaml没有看到我的ivalueconverter

时间:2016-02-02 17:25:27

标签: c# .net wpf visual-studio-2015 ivalueconverter

我知道之前已经问过这个问题,但我真的无法根据这些答案弄清楚这里发生了什么,因为以前的问题似乎已经将他们的ivalueconverters放在不同的类或文件下了。我的IValueConverter位于命名空间Proc中,但出于某种原因,我收到了错误:

  

名称" LatValueConverter"在命名空间中不存在   " CLR-名称空间:PROC"

     

名称" LongValueConverter"在命名空间中不存在   " CLR-名称空间:PROC"

代码用于加载外部文件并根据文件中指定的lat / lon值放置图像。应用程序构建,但图像没有显示,这告诉我此方法失败。 (仅显示以下相关代码)

此外,这些错误位于visual studio错误列表中,但在xaml编辑器中键入local:后,LatValueConverter和LongValueConverter都会显示在下拉菜单中。我尝试过清洁/重建,但错误仍然存​​在。关于如何解决这个问题的任何想法?

Visual Studio 2015 Update 1编辑: 我在使用Visual Studio 2015 Update 1时注意到它后写了这个问题。我在Visual Studio 2015中重新加载了项目(没有更新),没有错误!这似乎是Visual Studio 2015 Update 1的一个错误。任何人都知道一个解决方法吗?

这是xaml:

<Window x:Class="Proc.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:Proc"
    mc:Ignorable="d">
<Window.Resources>
    <ResourceDictionary>
        <local:LatValueConverter x:Key="latValueConverter" /> <!-- Errors are here -->
        <local:LongValueConverter x:Key="longValueConverter" /> <!-- Errors are here -->
        <sys:Double x:Key="mapWidth">950</sys:Double>
        <sys:Double x:Key="mapHeight">530</sys:Double>
    </ResourceDictionary>
</Window.Resources>

这是MainWindow.xaml.cs:

namespace Proc
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
      //...
    }
    public class LatValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double latitude = (double)value;
            double height = (double)parameter;

            int top = (int)((Constants.LatTop - latitude) / (Constants.LatTop - Constants.LatBottom) * height);
            return top;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class LongValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double longitude = (double)value;
            double width = (double)parameter;

            int left = (int)((Constants.LongLeft - longitude) / (Constants.LongLeft - Constants.LongRight) * width);
            return left;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

删除ResourceDictionary个对象;所以它应该看起来像这样:

<Window.Resources>
    <local:LatValueConverter x:Key="latValueConverter" /> <!-- Errors are here -->
    <local:LongValueConverter x:Key="longValueConverter" /> <!-- Errors are here -->
    <sys:Double x:Key="mapWidth">950</sys:Double>
    <sys:Double x:Key="mapHeight">530</sys:Double>
</Window.Resources>

答案 1 :(得分:0)

我在VS 2015 Update 1上遇到了同样的问题。

代码构建并运行正常,但设计师抱怨说&#34;某些元素还没有建成......&#34;如果我用鼠标悬停在&#34;冒犯&#34;工具提示显示的XAML的一部分&#34;命名空间等中不存在MyCustomConverter ...&#34;

出于某种原因,在我的情况下,只有当我将解决方案构建为任何CPU时,才会获得此设计器行为。如果我强制它到x64那么设计师就可以工作了。

我还没有尝试过VS 2015更新2。