【问题标题】:Changing xamarin.forms binding in viewmodel在 viewmodel 中更改 xamarin.forms 绑定
【发布时间】:2020-09-24 04:10:43
【问题描述】:

将数据上传到我的 API 时,我有两个单独的过程:#1。图片上传和#2。数据上传。

我在活动指示器下方放置了一个标签,上面写着 Submitting request...,出于 UI 目的,我希望它更改为 Uploading attachments...,完成后,将其更改为 Submitting application...

自从我一直在我的视图中分配绑定属性,但现在我想做的是在我的视图模型中分配值并让我的 xaml 读取它。这是我的工作:

<StackLayout IsVisible="{Binding FrameActInd}" Margin="0, 0, 0, 20" Padding="20">
     <ActivityIndicator IsRunning="True" BackgroundColor="White" Color="#62bef0" HorizontalOptions="Center"/>
     <Label Text="{Binding ActIndText}" TextColor="Black" HorizontalOptions="Center"/>
 </StackLayout>

ActIndtext 是我要更改的属性。这是我的视图模型

        string actIndText = "Submitting request...";
        public string ActIndText {
            get => actIndText;
            set {
                if (actIndText == value) {
                    return;
                } else {
                    actIndText = value;
                    OnPropertyChanged(nameof(ActIndText));

                }
            }
        }

这是我必须更改绑定属性值的视图模型部分:

            ...
            if (AttachmentsList.AttachmentPath.Count != 0) {
                ActIndText = "Uploading attachments...";
                foreach (var attachmentPath in AttachmentsList.AttachmentPath) {
                    UploadImage(attachmentPath, UserInfo.g_ClientID, tempAppNo, SLT_CODE);
                }
            }
            ActIndText = "Submitting application...";

            var httpClient = Globals.g_HttpClient;
            ...

但是在运行应用程序时它总是显示Submitting request...

【问题讨论】:

  • 我注意到您有两个独立的进程,您可以尝试通过Device.BeginInvokeOnMainThread (() =&gt; { ActIndText = "Uploading attachments..."; }); 更新ActIndText 值进行测试。
  • @LeonLu-MSFT 试过了,还是不行

标签: c# xamarin.forms binding


【解决方案1】:

问题是这段代码:

if (AttachmentsList.AttachmentPath.Count != 0) {
                ActIndText = "Uploading attachments...";
                foreach (var attachmentPath in AttachmentsList.AttachmentPath) {
                    UploadImage(attachmentPath, UserInfo.g_ClientID, tempAppNo, SLT_CODE);
                }
            }

因为您没有等待上传,所以它将开始上传所有图片。但它不会等到上传完成,而是会继续执行其余代码,即:

ActIndText = "Submitting application...";

这就是您看不到"Uploading attachments..." 的原因。如果您将此行注释掉,您将看到绑定有效,并在您的视图中显示 "Uploading attachments..."

【讨论】:

  • 现在有效。我在函数调用中添加了一个等待,它可以完美地工作。我还创建了一个函数来设置 ActIndtext public async Task UpdateActIndText(string text) { await Task.Delay(10); ActIndText = text; } 的值
猜你喜欢
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2014-05-05
  • 1970-01-01
  • 2015-03-21
  • 2018-08-07
  • 2021-12-10
  • 1970-01-01
相关资源
最近更新 更多