简体   繁体   English

字符串被神秘地切断了

[英]String gets mysteriously cut off

In my application I use WpfLocalization to provide translations while the application is running. 在我的应用程序中,我使用WpfLocalization在应用程序运行时提供翻译。 The library will basically maintain a list of properties and their assigned localization keywords and use DependencyObject.SetValue() to update their values when the active language is changed. 该库基本上维护属性列表及其分配的本地化关键字,并在更改活动语言时使用DependencyObject.SetValue()更新其值。

The scenario in which I noticed my problem is this: I have a simple TextBlock and have assigned a localization keyword for its Text property. 我注意到我的问题的情况是这样的:我有一个简单的TextBlock并为其Text属性分配了一个本地化关键字。 Now when my application starts, it will write the initial value into it and it will display just fine on screen. 现在,当我的应用程序启动时,它会将初始值写入其中,它将在屏幕上显示正常。 Now I switch the language and the new value is set as the Text property but only half the text will actually display on screen. 现在我切换语言,新值被设置为Text属性,但实际上只有一半的文本显示在屏幕上。 Switching the languages back and forth does not have any effect. 来回切换语言没有任何影响。 The first language is always displayed fine, the second is cut off (in the middle of words, but always full characters). 第一种语言总是显示正常,第二种语言被截断(在单词的中间,但总是完整的字符)。

The relative length of both languages to each other does not seem to have anything to do with it. 两种语言之间的相对长度似乎与它没有任何关系。 In my test case the working language string is 498 bytes and the one that gets cut off is 439 bytes and gets cut off after 257 bytes). 在我的测试用例中,工作语言字符串是498个字节,被切断的字符串是439个字节,在257个字节后被切断。

When I inspect the current value of the Text property of said TextBlock right before I change its value through the localization code, it will always have the expected value (not cut off) in either language. 当我在通过本地化代码更改其值之前检查所述TextBlockText属性的当前值时,它将始终具有任一语言的预期值(不会被截断)。

When inspecting the TextBlock at runtime through WPF Inspector it will display the cut off text as the Text property in the second language. 通过WPF Inspector在运行时检查TextBlock时,它将以第二种语言显示剪切文本作为Text属性。

This makes no sense to me at all thus far. 到目前为止,这对我来说毫无意义。 But now it gets better. 但现在它变得更好了。

The original WpfLocalization library reads the localized strings from standard resource files, but we use a modified version that can also read those string from an Excel file. 原始WpfLocalization库从标准资源文件中读取本地化字符串,但我们使用的修改版本也可以从Excel文件中读取这些字符串。 It does that by opening an OleDbConnection using the Microsoft OLE DB driver and reading the strings through that. 它通过使用Microsoft OLE DB驱动程序打开OleDbConnection并通过它读取字符串来实现。 In the debugger I can see that all the values are read just fine. 在调试器中,我可以看到所有的值都被读得很好。

Now I was really surprised when a colleague found the fix for the "cut off text" issue. 当一位同事找到“切断文本”问题的解决方案时,我真的很惊讶。 He re-ordered the rows in the Excel sheet. 他重新排序了Excel表格中的行。 I don't see how that could be relevant, but switching between the two versions of that file has an impact on the issue. 我不明白这是如何相关的,但在该文件的两个版本之间切换会对该问题产生影响。

That does actually make sense, it's because the ole db driver for Excel has to take a sample of the data in a column to assign it a type and in the case of string, also a length. 这确实有意义,这是因为Excel的ole db驱动程序必须在列中获取数据样本以为其分配类型,在字符串的情况下,也是长度。 If it only samples values below the 255 character threshold, you will get a string(255) type and truncated text, if it has sampled a longer string, it will assign it as a memo column and allow longer strings to be retrieved / stored. 如果它仅采样低于255个字符阈值的值,您将获得字符串(255)类型和截断文本,如果它已采样较长的字符串,则会将其指定为备注列并允许检索/存储更长的字符串。 By re-ordering, you are changing which rows are sampled. 通过重新排序,您将更改采样的行。

If you read the SQL Server to Excel using oledb you will find this is a known issue. 如果您使用oledb将SQL Server读取到Excel,您会发现这是一个已知问题。 http://msdn.microsoft.com/en-us/library/ms141683.aspx - since you are using the same ole db driver, I would expect the situation to also apply to you. http://msdn.microsoft.com/en-us/library/ms141683.aspx - 由于您使用相同的ole db驱动程序,我希望情况也适用于您。

From the docs: 来自文档:

Truncated text. 截断文本。 When the driver determines that an Excel column contains text data, the driver selects the data type (string or memo) based on the longest value that it samples. 当驱动程序确定Excel列包含文本数据时,驱动程序将根据其采样的最长值选择数据类型(字符串或备忘录)。 If the driver does not discover any values longer than 255 characters in the rows that it samples, it treats the column as a 255-character string column instead of a memo column. 如果驱动程序未在其采样的行中发现长度超过255个字符的任何值,则会将该列视为255个字符的字符串列而不是备注列。 Therefore, values longer than 255 characters may be truncated. 因此,可能会截断超过255个字符的值。 To import data from a memo column without truncation, you must make sure that the memo column in at least one of the sampled rows contains a value longer than 255 characters, or you must increase the number of rows sampled by the driver to include such a row. 要从备注列导入数据而不截断,必须确保至少一个采样行中的备注列包含长度超过255个字符的值,或者必须增加驱动程序采样的行数以包含此类行。 You can increase the number of rows sampled by increasing the value of TypeGuessRows under the HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Jet\\4.0\\Engines\\Excel registry key. 您可以通过增加HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Jet \\ 4.0 \\ Engines \\ Excel注册表项下的TypeGuessRows的值来增加采样的行数。 For more information, see PRB: Transfer of Data from Jet 4.0 OLEDB Source Fails w/ Error. 有关更多信息,请参阅PRB:从Jet 4.0 OLEDB源传输数据失败,错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM