XAML 看不到我的 iValueConverter

本文关键字:iValueConverter 我的 看不到 XAML | 更新日期: 2023-09-27 18:33:04

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

命名空间中不存在名称"LatValueConverter"clr-namespace:Proc"。

命名空间中不存在名称"LongValueConverter"clr-namespace:Proc"。

该代码旨在加载外部文件并根据文件中指定的纬度/纬度值放置图像。 应用程序生成,但图像未显示,这告诉我此方法失败。(仅在下面显示相关代码(

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

Visual Studio 2015 Update 1 EDIT:我在使用 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();
        }
    }
}

XAML 看不到我的 iValueConverter

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

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

代码构建并运行正常,但设计器抱怨"某些元素尚未构建......"如果我将鼠标悬停在 XAML 的"违规"部分,工具提示会显示"命名空间中不存在我的自定义转换器等..."

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

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