WPF项目报 未能找到程序集“<程序集名>”错误的解决方法

今天看书看到WPF的IValueconvertet接口的实现时,google下,想仔细学习下,看到一个例子,于是就按照作者的意思自己建立个项目(WPF项目),然后添加代码,原文章地址:点击打开链接,我贴在下面。大家也可以试试,你建立完项目工程,把代码复制进去后xaml会报未能找到程序集ValueConverterDemo,于是我有google下,在MSDN看到了解决方法,如下:

完整的错误信息:“确保已引用包含该类型的程序集。”如果该程序集是当前开发项目的一部分,请确保已生成了该项目。

装载设计器时,Visual Studio 无法找到类型。

更正此错误

  • 确保引用了包含此类型的程序集。如果该程序集是当前开发项目的一部分,则应确保该项目是通过选择“生成”菜单中的“生成解决方案”生成的。

简单吧,只要重新生成解决方案即可。

原贴内容

IValueConverter in WPF data binding

One of the handy things that you can do with data binding in WPF is that you convert the data as you pull it from the data source. The mechanism for this is the IValueConverter interface.
 
Let's say that you have a list of numbers representing positions in a race, and you want to display this list with the appropriate positional text (for example, 1 is 1st, 2 is 2nd, 3 is 3rd, etc). Rather than save your data in this form (which would not be convenient at all) you could leave the data as an int in the data source, and write a value converter to convert the integer into the appropriate string. This has many benefits - for example, if the UI is being virtualized, then only the items that have been created will be converted. And if the position changes WPF takes care of updating the string.
 
The first step is to make a class deriving from IValueConverter. We will call our class PositionConverter. There are two interface members - one of them to convert to the target type, and one of them to convert back. We don't need to convert back, so we just need to implement ConvertTo:

public class PositionConverter : IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

        {

            if (value != null)

            {

                int number = (int)value;

 

                string numberText = number.ToString();

                string positionText;

 

                if (number >= 10 && numberText[numberText.Length - 2] == '1')

                {

                    // teen numbers always end in 'th'

                    positionText = "th";

                }

                else

                {

                    switch (numberText[numberText.Length - 1])

                    {

                        case '1':

                            positionText = "st";

                            break;

                        case '2':

                            positionText = "nd";

                            break;

                        case '3':

                            positionText = "rd";

                            break;

                        default:

                            positionText = "th";

                            break;

                    }

                }

 

                return (numberText + positionText);

            }

 

            return string.Empty;

        }

 

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

        {

            throw new Exception("The method or operation is not implemented.");

        }

    }

To use the converter we can create one in the resources section of the window, and refer to it as a static resource in the binding statement:

<Window x:Class="ValueConverterDemo.Window1"

  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

  xmlns:local="clr-namespace:ValueConverterDemo"

  Title="ValueConverterDemo"

  Height="300"

  Width="300">

  <Window.Resources>

    <local:PositionConverter x:Key="PositionConverter" />

  </Window.Resources>

  <ListBox Name="numberBox">

    <ListBox.ItemTemplate>

      <DataTemplate>

        <TextBlock Text="{Binding Converter={StaticResource PositionConverter}}" />

      </DataTemplate>

    </ListBox.ItemTemplate>

  </ListBox>

</Window>

Now the numbers show up with positional text in them (45 becomes 45th, for example).

Value converters are very useful tools for letting you use the power of data binding. You can even pass parameters to them, so that you can combine data in the value converter.

Next up I show what ConvertBack is used for.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值