WPF:无法在 Window.Resources 中实例化类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23322591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:39:04  来源:igfitidea点击:

WPF: Can't instantiate class in Window.Resources

c#wpfxamlresources

提问by matt-pielat

I'm doing this WPF tutorial and for some reason I get an error when adding a custom SlidersToColorConverterclass to resources.

我正在做这个 WPF 教程,由于某种原因,我在向SlidersToColorConverter资源添加自定义类时出错。

Someone on StackOverflow was doing it exact same way.

StackOverflow 上的某个人正在以完全相同的方式做这件事。

MainWindow.xaml:

主窗口.xaml

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication2"
    Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <local:SlidersToColorConverter x:Key="whyareyounotworking"/>
    </Window.Resources>
</Window>

SlidersToColorConverter.cs:

SlidersToColorConverter.cs

namespace WpfApplication2
{
    class SlidersToColorConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double red = (double)values[0];
            double green = (double)values[1];
            double blue = (double)values[2];
            return new SolidColorBrush(Color.FromArgb(255, (byte)red, (byte)green, (byte)blue));
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Error List:

错误列表

The name "SlidersToColorConverter" does not exist in the namespace "clr-namespace:WpfApplication2". c:\users\mateusz\documents\visual studio 2013\Projects\WpfApplication2\WpfApplication2\MainWindow.xaml  39  9   WpfApplication2

回答by Jason Down

It looks like the class is private (by default). You must change your definition to

看起来这个类是私有的(默认情况下)。您必须将定义更改为

public class SlidersToColorConverter : IMultiValueConverter