gpt4 book ai didi

c# - 在 Windows Phone 8 应用程序中将转换器绑定(bind)到 XAML

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:59 25 4
gpt4 key购买 nike

我的 .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

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;
}
}
}

显示错误

"the name NotConverter does not exists in the namespace clr-namespace:MVVM1Test.Resources.Converters"

但是我也将转换器类添加到解决方案中。

最佳答案

当您将项目放入资源字典时,您必须为它们提供键。 x:Name 属性是多余的(除非您需要从代码后面访问该对象)。

<converter:NotConverter x:Key="not" />

此外,您遇到的错误可能是设计时问题。尝试在我建议的更改后编译并运行。

关于c# - 在 Windows Phone 8 应用程序中将转换器绑定(bind)到 XAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19510556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com