WPF:无法在 Window.Resources 中实例化类 [英] WPF: Can't instantiate class in Window.Resources

查看:87
本文介绍了WPF:无法在 Window.Resources 中实例化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

StackOverflow 上有人用完全相同的方式做这件事.

Someone on StackOverflow was doing it exact same way.

MainWindow.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>

SliderToColorConverter.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();
        }
    }
}

错误列表:

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

推荐答案

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

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

public class SlidersToColorConverter : IMultiValueConverter

这篇关于WPF:无法在 Window.Resources 中实例化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆