0

需要帮助解决这个问题。

我在 MainPage.xaml 中初始化一个文本框

<TextBox 
    x:Name="box"
    HorizontalAlignment="Left"
    TextWrapping="NoWrap"
    Text="TextBox"
    VerticalAlignment="Center"
    RenderTransformOrigin="1.332,-26.438"
    Height="23" Width="1366"
    HorizontalContentAlignment="Left">
</TextBox>

然后我尝试更改 MainPage.xaml.cs 中的边距:

box.Margin = new Thickness(0,grid.Height - 23,0,0);

这是运行时错误

An exception of type 'System.Runtime.InteropServices.COMException'
occurred in Console.exe but was not handled in user code

Additional information: Error HRESULT E_FAIL has been returned from
a call to a COM component.

这是显示边距更改代码和错误的图像: http: //screencast.com/t/U7pzizHzb

4

1 回答 1

0

好像你让这个过于复杂。您设置的边距将导致 TextBox 底部对齐并强制为 23 像素高。那么,为什么不一开始就这样做呢?

<TextBox ... Height="23" VerticalAlignment="Bottom" />

我不是 100% 确定是什么导致了您的 COM 错误,但我的第一个猜测是您的“手动更改边距”代码是在 UI 框架尚未准备好进行此类更改的时间点运行的。(我发现 WinRT 在这种情况下非常脆弱。)因此删除代码隐藏并在 XAML 中指定您的布局可能会修复您的 COM 错误,并使您的代码更简单和更易于维护。

于 2012-09-03T02:51:36.510 回答