【发布时间】:2017-02-05 16:26:04
【问题描述】:
我在启动 UWP 应用时收到此错误。它未能找到 FirstNameToVisibilityConverter 资源。如果有人能找出我收到此错误的原因,或者使用转换器发布一个 UWP 应用程序的小型工作示例,我将不胜感激。谢谢!
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();
}
}
}
【问题讨论】:
-
我没有看到代码有任何问题。您是否在转换器中尝试了 BreakPoint 并查看它是否命中?
-
NVM。但是您是否尝试将您的
{x:Bind}更改为{Binding}以查看它是否有效?{x:Bind}是编译时间,{Binding}是运行时间。