如何解决绑定文本需要在Xamarin中重新加载页面
我正在研究Xamarin.Forms应用程序。我在viewmodel页面中设置了一个条件,然后通过绑定更改了标签文本和图像按钮的可见性。
但是问题是,当我打开页面时,屏幕为空,但是当我刷新页面时,它会显示文本。
Indexviewmodel.cs
public override async Task InitializeAsync(object navigationData)
{
var firstname = await SecureStorage.GetAsync("firstname");
if (firstname == null)
{
var RegisteredUserID = await SecureStorage.GetAsync("RegisteredUserID");
_indexText = "Waiting for Identity to be Issued";
IsQRScannerVisible = false;
}
else
{
_indexText = "Your App is Ready";
IsQRScannerVisible = true;
}
await base.InitializeAsync(navigationData);
}
可绑定属性
private string _indexText;
public string IndexText
{
get => _indexText;
set => this.RaiseAndSetIfChanged(ref _indexText,value);
}
public bool IsQRScannerVisible { get; private set; }
IndexPage.xml
<Label
FontSize="14"
HorizontalOptions="Center"
Text="{Binding IndexText}"
TextColor="White"
Margin="0,-26,150">
</Label>
<ImageButton
Source="drawable/qrcode_icon.png"
HorizontalOptions="Center"
VerticalOptions="CenterandExpand"
WidthRequest = "70"
HeightRequest = "70"
MinimumHeightRequest = "70"
MinimumWidthRequest = "70"
BackgroundColor="#004B86"
IsVisible="{Binding IsQRScannerVisible}"
Command="{Binding ScanVerificationCommand}"/>
解决方法
您正在设置内部字段
_indexText = "Waiting for Identity to be Issued";
这样做会绕过PropertyChanged
事件。而是使用公共财产
IndexTest = "Waiting for Identity to be Issued";
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。