【问题标题】:Binding Text Needs to Reload the Page in Xamarin绑定文本需要在 Xamarin 中重新加载页面
【发布时间】:2021-01-10 00:24:22
【问题描述】:

我正在开发 Xamarin.Forms 应用程序。我在 ViewModel 页面中设置了一个条件,并通过绑定更改了标签文本和图像按钮的可见性。

但问题是,当我打开页面时,屏幕是空的,但是当我刷新页面时,它会显示文本。

以下是 xml 和 .cs 页面的代码。

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,0,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}"/>

【问题讨论】:

    标签: c# .net xamarin .net-core binding


    【解决方案1】:

    您正在设置内部字段

    _indexText = "Waiting for Identity to be Issued";
    

    这样做会绕过PropertyChanged 事件。而是使用公共财产

    IndexTest = "Waiting for Identity to be Issued";
    

    【讨论】:

    • 感谢文本部分工作,但图像按钮仍需要刷新
    • IsQRScannerVisible 没有打电话给RaiseAndSetIfChanged
    猜你喜欢
    • 2021-05-15
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多