Using MessagingCenter to update progressbar problem...

JKnottJKnott Member ✭✭✭

I have a progress bar I want to use to show the user that the app is not frozen, and let them know what is going on behind the scenes.
I am using a SyncFusion Popup as the display, but I am having a hard time figuring out how to go about getting the progressbar updated.

My code, to start the update and really long process is this

try
{
    PopupPage popup = new PopupPage();
    popup.Show();
            await DBHelper.DropFullDatabase();
            await DBHelper.InitDBStore();
    }

my ViewModel for the popup page contains this.

public class ProgressBarUpdatesViewModel
    {
        public int ProgressValue { get; set; }
        public string LabelText { get; set; }
        public ProgressBarUpdatesViewModel()
    {
                MessagingCenter.Subscribe<PopupPage, int>(this, "StepOnTheSteppingStepper",
                    (progressb, x) =>
                    {
                        this.ProgressValue += x;
                    });

                ProgressValue = 0;
                LabelText = "Test";
        }
}

MyXAML to show the progressbar looks like this

        <Label x:Name="Label" Text="{Binding Path=LabelText, Mode=TwoWay}" />
                    <progressBar:SfCircularProgressBar
                        x:Name="ProgB"
                        EndAngle="60"
                        Progress="{Binding Path=ProgressValue, Mode=TwoWay}"
                        StartAngle="120" />

And finally the popuppage code is here...

public partial class PopupPage : SfPopupLayout
    {
        ProgressBarUpdatesViewModel viewModel;
        public PopupPage()
        {
                InitializeComponent();
                BindingContext = viewModel = new ProgressBarUpdatesViewModel();

                MessagingCenter.Subscribe<PopupPage, int>(this, "StepOnTheSteppingStepper",
                    (progressb, x) =>
                    {
                        progressb.viewModel.ProgressValue += x;
                        if (progressb.viewModel.ProgressValue == 100)
                        {
                //Once the progress reaches 100% wait for two seconds, and dismiss and clean up.
                            Thread.Sleep(2000);
                            this.Dismiss();
                            this.Dispose();
                            MessagingCenter.Unsubscribe<PopupPage, int>(this, "StepOnTheSteppingStepper");
                        }
                    });
        }
}

Finally in my long running code, I have messages like this being published.

//Building database structures.
MessagingCenter.Send<PopupPage, int>(this, "StepOnTheSteppingStepper", 10);
    await Task.Delay(100);

Here's the problem though. The function I call to perform the database functions is a different class in a different namespace, so when I try to use that code, I get an error saying "cannot convert from DBHelper to PopupPage" I know there has to be something simple I am overlooking, but if someone could please key me into how I can make this work, I would be most grateful!

I'm using more or less the same code elsewhere to communicate from my popup back to the parent page elsewhere, so I am pretty sure this will work, but if anyone knows an easier or better method, I would love to hear that as well!

Cheers!

Best Answers

Answers

  • JKnottJKnott Member ✭✭✭

    @LandLu Thanks! That took care of the message delivery, and the type mismatch, but now for some reason my progress bar is not updating or reflecting the values being passed to it? Cal you recommend any fixes I could try?

    Here is my XAML

    <?xml version="1.0" encoding="utf-8" ?>
    <sfPopup:SfPopupLayout
          x:Class="MyApp.Services.PopupPage"
            xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:progressBar="clr-namespace:Syncfusion.XForms.ProgressBar;assembly=Syncfusion.SfProgressBar.XForms"
                xmlns:sfPopup="clr-namespace:Syncfusion.XForms.PopupLayout;assembly=Syncfusion.SfPopupLayout.XForms">
    
        <sfPopup:SfPopupLayout.PopupView>
            <sfPopup:PopupView
                ShowCloseButton="False"
                ShowFooter="False"
                ShowHeader="False">
                <sfPopup:PopupView.ContentTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <Label x:Name="Label" TextColor="Black" />
                            <progressBar:SfCircularProgressBar
                                x:Name="ProgB"
                                EndAngle="60"
                                Progress="{Binding Path=ProgressValue, Mode=TwoWay}"
                                ShowProgressValue="True"
                                StartAngle="120" />
                        </StackLayout>
                    </DataTemplate>
                </sfPopup:PopupView.ContentTemplate>
            </sfPopup:PopupView>
        </sfPopup:SfPopupLayout.PopupView>
    </sfPopup:SfPopupLayout>
    

    My initialization code for the view model.

    public ProgressBarUpdatesViewModel()
        {
    
            MessagingCenter.Subscribe<object, int>(this, "InitializingStepper",
                    (progressb, x) =>
                    {
                        this.ProgressValue += x;
                    });
    
            ProgressValue = 0;
            LabelText = "Test";
        }
    

    My popupPage CS Codebehind

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class PopupPage : SfPopupLayout
    {
            ProgressBarUpdatesViewModel viewModel;
            public PopupPage()
            {
                InitializeComponent();
                BindingContext = viewModel = new ProgressBarUpdatesViewModel();
                MessagingCenter.Subscribe<object, int>(this, "InitializingStepper",
                        (progressb, x) =>
                        {
                            this.viewModel.ProgressValue += x;
                        });
            }
        }
    

    Then in my long running code, I have these placed periodically.

    MessagingCenter.Send<object, int>(this, "InitializingStepper", 20);
    

    I can see the viewModel.ProgressValue updating via breakpoints, but the popup page it's self never updates. It just shows, 0 percent complete, and never moves.

    Sorry to keep pestering with this, I just have burnt out my thinker on this one over the last couple days.
    Is there a better way to address updating a progress bar?

    I'll mark your answer as the fix, but if you could give this part a shot I would really appreciate it!

    Cheers!

  • JKnottJKnott Member ✭✭✭

    OMG... LOL Well that's what I get for spinning my wheels on too many bugs at once... You'd think that after making nearly 20 other viewmodels in my project I would remember to type something as simple as " : BaseViewModel" but then again, I guess that would make too much sense....

    Thank you for the assist!

Sign In or Register to comment.