gpt4 book ai didi

c# - Xaml TextBlock 中的中间词丢失,但最后一个词不存在

转载 作者:行者123 更新时间:2023-11-30 21:37:44 25 4
gpt4 key购买 nike

我尝试在 xaml 中显示一些文本,但看起来它在中间被截断了,但下一个词出现了。这是我的 xaml。

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="115" />
</Grid.ColumnDefinitions>
<TextBlock TextWrapping="Wrap" HorizontalAlignment="Center" FontFamily="Arial" FontSize="13.33333333">ABCDE IAATA Corp.</TextBlock>
</Grid>

Which results in this .请注意中间词 (IAATA) 丢失了,但最后一个词 (Corp.) 在那里。知道这里发生了什么吗?我希望第三个词会换行,或者第二个词会出现,最后一个词被截断,甚至第二个和第三个词都被截断,但不是这个。

我能够在 VS 和 Kaxaml 中对此进行测试

更新对于更多上下文,进入的 xaml 是从其他地方生成的。稍后我对 xaml 元素进行了一些测量,这会引发“FatalExecutionEngineError”异常,该异常会立即终止应用程序。

消息是

Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 
'C:\Projects\TestFailFast\TestFailFast\bin\Debug\TestFailFast.vshost.exe'.

Additional information: The runtime has encountered a fatal error. The address of the error was at 0x583ee28b, on thread 0x2af0. The error code is 0x80131623. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack

并且通过事件查看器日志获得的堆栈跟踪是

Application: Services.BackEnd.exe Framework Version: v4.0.30319 Description: The application requested process termination through System.Environment.FailFast(string message). Message: Unrecoverable system error. Stack: at   System.Environment.FailFast(System.String) at   System.Windows.Controls.TextBlock.IsAtCaretUnitBoundary(System.Windows.Documents.ITextPointer, Int32, Int32) at   System.Windows.Documents.TextContainer.IsAtCaretUnitBoundary(System.Windows.Documents.TextPointer) at   System.Windows.Documents.TextPointer.System.Windows.Documents.ITextPointer.get_IsAtCaretUnitBoundary() at System.Windows.Documents.TextPointerBase.IsAtCaretUnitBoundary(System.Windows.Documents.ITextPointer) at System.Windows.Documents.TextPointerBase.MoveToNextInsertionPosition(System.Windows.Documents.ITextPointer, System.Windows.Documents.LogicalDirection) at   System.Windows.Documents.TextPointer.System.Windows.Documents.ITextPointer.GetNextInsertionPosition(System.Windows.Documents.LogicalDirection) ...

This is only exposed if I have the above xaml wrapped in a FixedDocument like

<FixedDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<PageContent>
<FixedPage>
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="115" />
</Grid.ColumnDefinitions>
<TextBlock Text="ABCDE IAATA Corp." FontFamily="Arial" FontSize="13.333333" TextWrapping="Wrap" HorizontalAlignment="Center"/>
</Grid>
</FixedPage>
</PageContent>
</FixedDocument>

我这里有一个样本复制器

class Program {
[STAThread]
static void Main(string[] args) {
var fixedPageXaml = @"<FixedDocument xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<PageContent>
<FixedPage>
<Grid xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=""115"" />
</Grid.ColumnDefinitions>
<TextBlock Text=""ABCDE IAATA Corp."" FontFamily=""Arial"" FontSize=""13.333333"" TextWrapping=""Wrap"" HorizontalAlignment=""Center""/>
</Grid>
</FixedPage>
</PageContent>
</FixedDocument>";

//Search through all xaml elements untill the TextBlock is found
var textBlock = FindTextBlock(XamlReader.Parse(fixedPageXaml));
var inline = textBlock.Inlines.FirstInline;

//Get a TextPointer to the end of a line that is specified relative to the current text pointer
// Add 1 to get the beginning of the line following the one count is requesting
var nextLineStart = inline.ElementStart.GetLineStartPosition(0 + 1);

if (nextLineStart != null) {
//Get the insertion position at the end of the document
//CRASH!
var nextPos = nextLineStart.GetNextInsertionPosition(LogicalDirection.Backward);
}


Console.WriteLine("Done");
Console.Read();
}

private static TextBlock FindTextBlock(object root) {
var fe = root;
while (fe.GetType() != typeof(TextBlock)) {
if (fe is FixedDocument) {
foreach (var p in ((FixedDocument)fe).Pages) {
var ret = FindTextBlock(p);
if (ret != null) {
return ret;
}
}
} else if (fe is FixedPage) {
foreach (UIElement c in ((FixedPage)fe).Children) {
var ret = FindTextBlock(c);
if (ret != null) {
return ret;
}
}
} else if (fe is PageContent) {
fe = ((PageContent)fe).Child;
} else if (fe is Grid) {
foreach (UIElement c in ((Grid)fe).Children) {
var ret = FindTextBlock(c);
if (ret != null) {
return ret;
}
}
}
}
if (fe.GetType() == typeof(TextBlock)) {
return fe as TextBlock;
} else {
return null;
}
}
}

这已在 .net 版本 4.6.2 中测试

最佳答案

如果您希望您的文本 block 与父文本 block 具有相同的宽度,请不要将 Horizo​​ntalAlignment 设置为 Left。作为网格子项,它将默认为 Stretch,这正是您想要的。如果需要,在 TextBlock 上设置 TextAlignment 以对齐文本。

我想知道这对于换行算法中的算法来说是否不是一个有趣的极端情况,其中“丢失”的文本只是放在看不见的地方:如果我将 FontSize 调低到 13.256(但不是 13.257),它换行适本地。如果我拨到 13.341(但不是 13.3405),它会正确换行。

关于c# - Xaml TextBlock 中的中间词丢失,但最后一个词不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46938827/

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