【发布时间】:2016-01-14 22:04:38
【问题描述】:
我有一个简单的WPF 页面,我在其中添加了一个本地资源,该资源在页面的后端文件中有其定义。
<Page x:Class="WindowsSampleApplication.ValueConverter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WindowsSampleApplication"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="ValueConverter">
<Page.Resources>
<local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" />
</Page.Resources>
<Grid>
<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>
</Grid>
</Page>
这是Page的后端文件
namespace WindowsSampleApplication
{
public partial class ValueConverter : Page
{
public ValueConverter()
{
InitializeComponent();
}
public class YesNoToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch (value.ToString().ToLower())
{
case "yes":
return true;
case "no":
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";
}
}
}
}
我已在Page 级别中正确包含命名空间WindowsSampleApplication,并且相同的命名空间具有我的资源的定义。但是当添加local resource时,我会低于error
我刚刚开始使用来自 here 的 WPF 教程,但我唯一尝试而不是遵循教程的是我添加了一个 页面 而不是 Window,因为我已经创建了一个Window,并在那里提供了早期的演示,希望我已经转换了 Page 所需的所有内容,而不是 Window。有人知道如何解决这个问题吗?
更新
我也尝试过将<local..../> 与<ResourceDictionary> 包装在一起,因为我在 SO 中准备好了它,但没有多大用处,错误仍然存在。
【问题讨论】: