【发布时间】:2022-01-26 19:42:18
【问题描述】:
我正在编写 C# UWP。我有一个带有以下示例代码的 UserControl:
<UserControl
x:Class="Sample.AllocPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="Sample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Name="Root"
mc:Ignorable="d">
<RelativePanel>
<AutoSuggestBox
Width="{Binding Mode=OneWay, Path=ActualWidth, ElementName=Root}"
Margin="50,0,50,0"
RelativePanel.AlignHorizontalCenterWithPanel="True"
RelativePanel.AlignVerticalCenterWithPanel="True" />
</RelativePanel>
</UserControl>
此代码有效。但是,我想使用较新的x:Bind,所以我将{Binding Mode=OneWay, Path=ActualWidth, ElementName=Root} 替换为{x:Bind Mode=OneWay, Path=Root.ActualWidth}。现在由于某种原因,整个 AutoSuggestBox 都从屏幕上消失了,我假设它的宽度是 0。
为什么会发生这种情况,我该如何解决?
【问题讨论】:
-
这可能是由于 Binding 和 {x:Bind} 之间的差异造成的。 {x:Bind} 执行在编译时生成的专用代码,而 {Binding} 使用通用运行时对象检查。您可能需要在
UserControl中实现INotifyPropertyChanged接口。
标签: c# xaml uwp winrt-xaml