fxsee3 2023-09-01 12:20 采纳率: 0%
浏览 10

c# 关于转换器的错误,求怎么解决

关于转换器的错误,求怎么解决
新建一个WPF 项目 WpfApp1,创建了MarginConverter.cs 文件

using System;
using System.Collections.Generic;
using System.Globalization;
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 WpfApp1
{
    /// <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";
        }
    }
}

在MainWindow.xaml里

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" 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 ElementName=txtValue, Path=Text, Converter={StaticResource YesNoToBooleanConverter}}"></TextBlock>
        </WrapPanel>
        <CheckBox IsChecked="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource YesNoToBooleanConverter}}" Content="Yes" />
    </StackPanel>
</Window>


出现错误:
错误 XDG0008 命名空间“clr-namespace:WpfApp1”中不存在“YesNoToBooleanConverter”名称。 WpfApp1 H:\VS2017EXE\VodMusic\WpfApp1\MainWindow.xaml 10
错误 XDG0062 无法解析资源“YesNoToBooleanConverter”。 WpfApp1 H:\VS2017EXE\VodMusic\WpfApp1\MainWindow.xaml 18

  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-09-01 14:09
    关注

    【以下回答由 GPT 生成】

    问题: 转换器的错误是什么?如何解决?

    答案: 转换器的错误在于没有实现IValueConverter接口的namespace引用。

    解决方法如下: 1. 在转换器文件的开头添加以下引用语句:

    using System.Globalization;
    using System.Windows.Data;
    
    1. 确保转换器类上面有正确的命名空间声明:
    namespace WpfApp1
    {
        public class YesNoToBooleanConverter : IValueConverter
        {
            // 转换器的代码
        }
    }
    
    1. 在XAML文件中使用转换器时,确保正确指定并引用转换器的命名空间:
    xmlns:local="clr-namespace:WpfApp1"
    

    并在需要使用转换器的地方使用local前缀:

    <Window.Resources>
        <local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" />
    </Window.Resources>
    
    1. 在需要使用转换器的地方,使用Binding对应的Converter属性为转换器:
    <TextBox Text="{Binding Name, Converter={StaticResource YesNoToBooleanConverter}}" />
    

    通过以上步骤,应该能够解决转换器的错误。如果还有其他问题或错误信息,请提供详细信息以便我可以更好地帮助您。


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 9月1日

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大