为什么 C# 接口没有使用明显正确的命名空间来实现?
Why is C# interface not implementing with apparently correct namespace?
我是一个完全的 C#(和 OOP)新手,所以也许这是一个非常简单的问题,但我被难住了。
我正在尝试编写一个实现 IValueConverter 接口的方法,绑定到一个简单的 XAML GUI。它不会编译,VS 告诉我:
Error XLS0414 The type 'local:YesNoToBooleanConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Error XDG0008 The name "YesNoToBooleanConverter" does not exist in the namespace "clr-namespace:ValueConverter".
Error CS0535 'MainWindow.YesNoToBooleanConverter' does not implement interface member 'IValueConverter.ConvertBack(object, Type, object, CultureInfo)'
Error CS0535 'MainWindow.YesNoToBooleanConverter' does not implement interface member 'IValueConverter.ConvertBack(object, Type, object, CultureInfo)'
我不明白的是CS0535错误。 Convert() 和 ConvertBack() 参数与 MS 文档完全匹配,据我所知,我添加了正确的命名空间,并且我已经清理并重建了项目。我还没有看到直接解决这个问题,所以要么我的理解存在根本性缺陷,要么我的机器运行异常。
有人指导吗?
我的 C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ValueConverter
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public class YesNoToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch (value.ToString().ToLower())
{
case "yes":
case "oui":
return true;
case "no":
case "non":
return false;
}
return false;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
if ((bool)value == true)
{
return "yes";
}
else
{
return "no";
}
}
return "no";
}
}
}
我的XAML:
<Window x:Class="ValueConverter.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:local="clr-namespace:ValueConverter"
mc:Ignorable="d"
Title="Converter Sample" Height="450" Width="800">
<Window.Resources>
<local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" />
</Window.Resources>
<StackPanel Margin="10">
<TextBox Name="txtValue" />
<WrapPanel Margin="0,10">
<TextBlock Text="Current value is: "/>
<TextBlock Text="{Binding Path=Text, ElementName=txtValue, Converter={StaticResource YesNoToBooleanConverter}}" />
</WrapPanel>
<CheckBox IsChecked="{Binding Path=Text, ElementName=txtValue, Converter={StaticResource YesNoToBooleanConverter}}" Content="Yes" />
</StackPanel>
您的 ConvertBack 方法嵌套在 MainWindow 下,而不是在 YesToNoBooleanConverter 下。
另外,因为 YesToNoBooleanConverter 嵌套在 MainWindow 中,您将不得不引用 local:MainWindow.YesToNoBooleanConverter
。将其移出成为自己的 class 而不是嵌套的 class
可能更有意义
我是一个完全的 C#(和 OOP)新手,所以也许这是一个非常简单的问题,但我被难住了。
我正在尝试编写一个实现 IValueConverter 接口的方法,绑定到一个简单的 XAML GUI。它不会编译,VS 告诉我:
Error XLS0414 The type 'local:YesNoToBooleanConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Error XDG0008 The name "YesNoToBooleanConverter" does not exist in the namespace "clr-namespace:ValueConverter".
Error CS0535 'MainWindow.YesNoToBooleanConverter' does not implement interface member 'IValueConverter.ConvertBack(object, Type, object, CultureInfo)'
Error CS0535 'MainWindow.YesNoToBooleanConverter' does not implement interface member 'IValueConverter.ConvertBack(object, Type, object, CultureInfo)'
我不明白的是CS0535错误。 Convert() 和 ConvertBack() 参数与 MS 文档完全匹配,据我所知,我添加了正确的命名空间,并且我已经清理并重建了项目。我还没有看到直接解决这个问题,所以要么我的理解存在根本性缺陷,要么我的机器运行异常。
有人指导吗?
我的 C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ValueConverter
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public class YesNoToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch (value.ToString().ToLower())
{
case "yes":
case "oui":
return true;
case "no":
case "non":
return false;
}
return false;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
if ((bool)value == true)
{
return "yes";
}
else
{
return "no";
}
}
return "no";
}
}
}
我的XAML:
<Window x:Class="ValueConverter.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:local="clr-namespace:ValueConverter"
mc:Ignorable="d"
Title="Converter Sample" Height="450" Width="800">
<Window.Resources>
<local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" />
</Window.Resources>
<StackPanel Margin="10">
<TextBox Name="txtValue" />
<WrapPanel Margin="0,10">
<TextBlock Text="Current value is: "/>
<TextBlock Text="{Binding Path=Text, ElementName=txtValue, Converter={StaticResource YesNoToBooleanConverter}}" />
</WrapPanel>
<CheckBox IsChecked="{Binding Path=Text, ElementName=txtValue, Converter={StaticResource YesNoToBooleanConverter}}" Content="Yes" />
</StackPanel>
您的 ConvertBack 方法嵌套在 MainWindow 下,而不是在 YesToNoBooleanConverter 下。
另外,因为 YesToNoBooleanConverter 嵌套在 MainWindow 中,您将不得不引用 local:MainWindow.YesToNoBooleanConverter
。将其移出成为自己的 class 而不是嵌套的 class