我在启动UWP应用程序时遇到了此错误。它无法找到FirstNameToVisibilityConverter资源。如果有人能够确定我为什么会出现这个错误,或者发布一个使用转换器的UWP应用程序的小型工作示例,我将不胜感激。谢谢!
XAML:
XAML:
<UserControl
x:Class="MyHelloWorld.HelloWorld"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyHelloWorld"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<Grid.Resources>
<local:FirstNameToVisibilityConverter x:Key="FirstNameToVisibilityConverter"/>
<p:Style TargetType="TextBox">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</p:Style>
</Grid.Resources>
<StackPanel>
<TextBlock Foreground="Red">HI</TextBlock>
<TextBlock Foreground="Red">THERE</TextBlock>
<TextBox Foreground="Red" Text="{x:Bind FirstWord}"/>
<TextBlock Foreground="Red" Text="{x:Bind SecondWord}" Visibility="{x:Bind FirstWord, Converter={StaticResource FirstNameToVisibilityConverter}}"/>
<CheckBox Foreground="Red" Content="Click me to hide the first word" IsChecked="{x:Bind FirstWordChecked}"/>
</StackPanel>
</Grid>
代码后台:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace MyHelloWorld
{
public sealed partial class HelloWorld : UserControl
{
public string FirstWord { get; set; }
public string SecondWord { get; set; }
public bool? FirstWordChecked { get; set; }
public HelloWorld()
{
this.InitializeComponent();
}
}
public class FirstNameToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if ((string)value == "Today") return Visibility.Collapsed;
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}
{x:Bind}
更改为{Binding}
看看是否有效吗?{x:Bind}
是编译时绑定,而{Binding}
是运行时绑定。 - AVK