【问题标题】:How to create add WinRT objects in C++/CX at runtime?如何在运行时在 C++/CX 中创建添加 WinRT 对象?
【发布时间】:2013-03-01 08:26:06
【问题描述】:

我想在一个函数(比如一个事件)中创建一个 WinRT 对象(比如一个文本块),然后在 C++/CX 中将它添加到一个页面(比如一个在运行时设置其行号和列号的网格)。有可能吗?

【问题讨论】:

  • 我不明白为什么这个问题会导致声誉下降

标签: c++-cx


【解决方案1】:

只需像调用任何其他对象一样在类上调用“ref new”,然后将其添加到网格的“Children”集合中。

为了设置网格的行列,需要在网格上调用SetRow/SetColumn来设置附加属性。

这很容易通过在 xaml 文件中命名网格(使用 x:Name 属性)来完成,这样您就可以在代码中按名称引用它。 Xaml:

<Grid x:Name="myGrid">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
</Grid>

代码隐藏:

auto textBlock = ref new TextBlock();
textBlock->Text = "this is my text";
myGrid->Children->Append(textBlock);
myGrid->SetRow(textBlock, 1);
myGrid->SetColumn(textBlock, 0);

【讨论】:

  • 以及如何设置属性? textBlock->VerticalAlignment=Center 不起作用。
  • 在代码中,VerticalAlignment 属性的类型为 Windows::UI::Xaml::VerticalAlignment。因此,您需要使用该类型的枚举来设置属性。 C++ 名称隐藏也会造成问题,因为页面可能有自己的名为 VerticalAlignment 的成员,因此您需要限定类型名称。 textBlock-&gt;VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Center;
  • 很抱歉我没有资格投票,否则我肯定会这样做
猜你喜欢
  • 2022-11-02
  • 2021-08-27
  • 2012-05-28
  • 2021-04-05
  • 2015-05-29
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
相关资源
最近更新 更多