C# 在windows phone 8应用程序中将转换器绑定到XAML
my.xaml页面代码:C# 在windows phone 8应用程序中将转换器绑定到XAML,c#,wpf,xaml,windows-phone-8,windows-phone,C#,Wpf,Xaml,Windows Phone 8,Windows Phone,my.xaml页面代码: <phone:PhoneApplicationPage x:Class="MVVM1Test.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Pho
<phone:PhoneApplicationPage
x:Class="MVVM1Test.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converter="clr-namespace:MVVM1Test.Resources.Converters"
mc:Ignorable="d"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<converter:NotConverter x:Name="not"></converter:NotConverter>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Gray">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
</phone:PhoneApplicationPage>
它显示了一个错误
“名称NotConverter在命名空间clr命名空间中不存在:MVVM1Test.Resources.Converters”
但是我也在解决方案中添加了converter类 这可能对你有帮助
xmlns:MyAppConverters=“clr命名空间:MyApp.Converters”
及
我已经给出了一个示例,您可以将类名更改为您的当您在资源字典中放置项时,必须为它们提供键。
x:Name
属性是多余的(除非您需要从代码隐藏访问对象)
此外,您得到的错误可能是设计时的问题。尝试编译并在我建议的更改后运行。我也遇到了类似的名称空间问题。对我来说,重启VisualStudio似乎解决了这个问题。当然,您必须确保在IValueConverter类中正确定义了名称空间 e、 g名称空间MVVM1Test.Resources.Converters
保存并重新启动visual studio。我已经将此文件添加到我的xaml文件中,作为:xmlns:converter=“clr namespace:MVVM1Test.Resources.Converters”编译项目帮助!
namespace MVVM1Test.Resources.Converters
{
public class NotConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !(bool)value;
}
}
}
<phone:PhoneApplicationPage.Resources>
<MyAppConverters:CustomConverter x:Key="customConverter"/>
</phone:PhoneApplicationPage.Resources>
<converter:NotConverter x:Key="not" />