我知道以前已经有人问过这个问题,但根据这些答案,我真的无法弄清楚这里发生了什么,因为似乎以前的提问者已将他们的 ivalueconverters 放在不同的类或文件下。我的 IValueConverter 位于 namespace Proc 中,但由于某种原因,我收到错误:
The name "LatValueConverter" does not exist in the namespace "clr-namespace:Proc".
The name "LongValueConverter" does not exist in the namespace "clr-namespace:Proc".
该代码旨在加载外部文件并根据文件中指定的纬度/经度值放置图像。应用程序构建,但图像没有显示,这告诉我此方法失败。 (下面仅显示相关代码)
此外,这些错误位于 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();
}
}
}
最佳答案
删除您的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>
关于c# - xaml 没有看到我的 ivalueconverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35159927/