gpt4 book ai didi

xaml - 无法将字符串转换为字符串

转载 作者:行者123 更新时间:2023-12-03 03:05:07 25 4
gpt4 key购买 nike

我的 xaml 中有一个文本框。
但我在导航到其他页面时收到此消息。
当输入、焦点和失去焦点时,动态监控文本框。
(检测是否包含文本并更改文本颜色。)
一切正常,我不知道异常会做什么。

这是文本框xaml

<TextBox Name="SearchBox" TextChanged="OnTextChanged" Height="72" InputScope="Search" GotFocus="OnGotFocus" KeyDown="OnKeyDown" LostFocus="OnLostFocus"
Text="{Binding LocalizedResources.Desc_InputKey, Mode=TwoWay, Source={StaticResource LocalizedStrings}}" >
<TextBox.Foreground>
<SolidColorBrush x:Name="SearhBoxColor" Color="{StaticResource PhoneTextBoxReadOnlyColor}"/>
</TextBox.Foreground>
</TextBox>

这是抛出的异常:

System.Windows.Data Error: ConvertBack cannot convert value 'hhh' (type 'System.String'). BindingExpression: Path='LocalizedResources.Desc_InputKey' DataItem='MyProject.LocalizedStrings' (HashCode=15848707); target element is 'System.Windows.Controls.TextBox' (Name='SearchBox'); target property is 'Text' (type 'System.String')..
System.ArgumentException: Property set method not found.
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at System.Windows.CLRPropertyListener.set_Value(Object value)
at System.Windows.PropertyAccessPathStep.set_Value(Object value)
at System.Windows.Data.BindingExpression.UpdateValue().

如何摆脱它?

最佳答案

您正在尝试使用双向绑定(bind)来绑定(bind)本地化资源中的文本。它无法工作,因为这些资源是只读的。

我相信您只是想设置文本框的初始值。因此,您应该绑定(bind)到您自己的属性,并使用资源对其进行初始化。

首先,在 View 模型中创建属性:

public string SearchBoxColorText { get; set; }

在代码中的某个位置初始化该属性(在类构造函数中,在 OnNavigateTo 事件中,无论适合您的工作流程):

this.SearchBoxColorText = LocalizedStrings.StaticlocalizedResources.Desc_InputKey;

然后将文本框绑定(bind)到此属性:

<TextBox Name="SearchBox" Text="{Binding SearchBoxColorText}" >

关于xaml - 无法将字符串转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14674070/

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