我是一個 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>
uj5u.com熱心網友回復:
您的 ConvertBack 方法嵌套在 MainWindow 下,而不是在 YesToNoBooleanConverter 下。
另外因為 YesToNoBooleanConverter 嵌套在 MainWindow 中,所以您必須參考local:MainWindow.YesToNoBooleanConverter. 將其移出成為自己的類而不是嵌套類可能更有意義
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/433602.html
下一篇:VB網路控制臺回圈
