wpf 在 Windows Phone 8 应用程序中将转换器绑定到 XAML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19510556/
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
Binding converter to XAML in windows phone 8 app
提问by Kuldeep Singh
my .xaml page code:
我的 .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.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.cs
notconverter.cs
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;
}
}
}
it is showing an error
它显示一个错误
"the name NotConverter does not exists in the namespace clr-namespace:MVVM1Test.Resources.Converters"
“名称 NotConverter 在名称空间 clr-namespace:MVVM1Test.Resources.Converters 中不存在”
But i have added the converter class also to the solution.
但我也在解决方案中添加了转换器类。
回答by Eli Arbel
When you place items in a resource dictionary, you must give them keys. The x:Nameproperty is redundant (unless you need to access the object from code behind).
当您将项目放入资源字典时,您必须为它们提供键。该x:Name属性是多余的(除非您需要从后面的代码访问该对象)。
<converter:NotConverter x:Key="not" />
Also, the error you're getting could be a design-time problem. Try to compile and run after the change I suggested.
此外,您遇到的错误可能是设计时问题。尝试在我建议的更改后编译并运行。
回答by Vykthur D .
I had a similar name space problem. For me, restarting visual studio seemed to solve the problem. Ofcourse, you must ensure the namespace is correctly defined in your IValueConverter Class e.g namespace MVVM1Test.Resources.Converters
我有一个类似的名称空间问题。对我来说,重新启动 Visual Studio 似乎解决了这个问题。当然,您必须确保在您的 IValueConverter 类中正确定义了命名空间,例如命名空间 MVVM1Test.Resources.Converters
Save and restart visual studio.
保存并重新启动visual studio。
回答by Debashrita
This may help you
这可能会帮助你
xmlns:MyAppConverters="clr-namespace:MyApp.Converters"
xmlns:MyAppConverters="clr-namespace:MyApp.Converters"
and
和
<phone:PhoneApplicationPage.Resources>
<MyAppConverters:CustomConverter x:Key="customConverter"/>
</phone:PhoneApplicationPage.Resources>
I have given the example you can change the class name as yours
我已经给出了示例,您可以将类名更改为您的

