我正在撰寫 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。
為什么會發生這種情況,我該如何解決?
uj5u.com熱心網友回復:
正如 Roy Li - MSFT 所說,您可能必須INotifyPropertyChanged在您的類(UserControl.xaml.cs)中實作介面。但在你這樣做之前,你需要在該類中添加 2 個新的命名空間。
第一個是System.ComponentModel(獲得訪問權INotifyPropertyChanged)第二個是System.Runtime.CompilerServices(獲得訪問權CallerMemberNameAttribute)
所以你的班級現在應該是這樣的
public sealed partial class MyUserControl1 : UserControl,INotifyPropertyChanged
{
public MyUserControl1()
{
this.InitializeComponent();
}
//some your code
public double _width;
public double width
{
get
{
_width = ActualWidth;
return _width;
OnPropertyChanged();
}
set
{
_width = value;
OnPropertyChanged();
}
}
public void OnPropertyChanged([CallerMemberName] string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
你的 Page.xaml 像這樣
<Grid>
<RelativePanel>
<AutoSuggestBox
x:Name="AutoSuggestBox"
Width="{x:Bind width, Mode=OneWay}"
Margin="50,0,50,0"
RelativePanel.AlignHorizontalCenterWithPanel="True"
RelativePanel.AlignVerticalCenterWithPanel="True" />
</RelativePanel>
</Grid>
兩天前我遇到了同樣的麻煩,所以我希望它會起作用。:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/415016.html
標籤:
下一篇:PytestHTML不顯示影像
