【问题标题】:xaml is not seeing my ivalueconverterxaml 没有看到我的 ivalueconverter
【发布时间】:2016-02-02 17:25:27
【问题描述】:

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

名称空间中不存在名称“LatValueConverter” “clr 命名空间:过程”。

名称空间中不存在名称“LongValueConverter” “clr 命名空间:过程”。

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

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

Visual Studio 2015 更新 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();
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

    删除您的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 更新 1 的错误。
    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多