简体   繁体   中英

Changing xamarin.forms binding in viewmodel

I have two separate process when uploading data to my API: #1. Image upload and #2. Data upload.

I've put a label below an activity indicator that says Submitting request... , for UI purposes, I want it to change to Uploading attachments... and when it's done, change it to Submitting application...

Ever since I've been assigning the binding property in my view, but now what I want to do is assign the value in my viewmodel and have my xaml read it. Here is what I've worked with:

<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 is the property I want to change. This is my viewmodel

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

                }
            }
        }

This is the portion of my viewmodel that I have to change the binding property's value:

            ...
            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;
            ...

But when running the application it always says Submitting request...

The problem is this code:

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

Because you are not awaiting the upload, it will start uploading all images. But it won't wait until the upload is finished, instead it will just resume executing the rest of the code, which is:

ActIndText = "Submitting application...";

This is why you don't see "Uploading attachments..." . If you comment this line out, you will see that the binding works and it shows "Uploading attachments..." in your view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM