question

WyllisMonteiro-6714 avatar image
0 Votes"
WyllisMonteiro-6714 asked WyllisMonteiro-6714 commented

Avoid xaml dependency in a service ?

I would like to refactor my viewmodel that allows to upload images on my server. I have a lot of functions DisplayImage(), UploadAllImages(), UploadSinglePicture(), MakeHTTPUploadRequest(), _restartUploasWhenRequestFailed() etc, and I want to put some functions into a class service called UploadService. But in the previous functions I have some part that calls view elements (`Label, StackLayout, Frame, ListView`), and I don't know if it's a good aproach. Can your tell me if view element can be in a service ? Do you think I should use a service in my view model that pass to service viewmodel himself. Let me show you an example

 public class UploadViewModel : INotifyPropertyChanged
 {
    private List<CachedImage> _listViewItemsSource;
    public List<CachedImage> ListViewItemsSource
    {
       get => _listViewItemsSource;
       set
       {
          _listViewItemsSource = value;
          OnPropertyChanged();
       }
    }
    
    private bool _restartUploadBtnIsVisible;
    public bool RestartUploadBtnIsVisible
    {
       get => _restartUploadBtnIsVisible;
       set
       {
          _restartUploadBtnIsVisible = value;
          OnPropertyChanged();
       }
    }
    
    private Page _page;
       
    public UploadViewModel(Page page) {
       _page = page;
       RestartUploadBtnIsVisible = false;
       ListViewItemsSource = List<CachedImage>() { };
       _ = StartUpload();
    }
    
    // I WOULD LIKE TO PUT THIS FUNCTION INTO A SERVICE
    async public Task StartUpload() {
       ListViewItemsSource = await api.GetImagesToUpload();
       for (int imageIndex = 0; imageIndex < ListViewItemsSource.Count; imageIndex++)
       {
         try
         {
           // I WOULD LIKE TO PUT THIS FUNCTION INTO A SERVICE TOO
           await UploadSinglePicture(imageIndex);
         }
         catch (Exception ex)
         {
           RestartUploadBtnIsVisible = true;
           _page.DisplayAlert("Error", "Error occured while uploading", "OK");
         }
      } 
    }
 }

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered WyllisMonteiro-6714 commented

Hello,

Welcome to our Microsoft Q&A platform!

I think your idea is correct, you could put functions into a class service even though it calls view elements.
You could try to create a static class named UploadService, pass value and get retuened value in StartUpload() method , then you could call this method by UploadService.StartUpload(), you could refer to the following code:

 public static class UploadService
     {
         public static async Task<string> StartUpload(List<string> ListViewItemsSource)// pass List<string>, return string
         {
             for (int imageIndex = 0; imageIndex < ListViewItemsSource.Count; imageIndex++)
             {
                 try
                 {
                     await UploadSinglePicture(imageIndex);
                 }
                 catch (Exception ex)
                 {
                     return "Error occured while uploading";
                 }
             }
             return "";
         }
         private static Task UploadSinglePicture(int imageIndex)
         {
             throw new NotImplementedException();
         }
     }

call the method

  var a = await UploadService.StartUpload(new List<string>());//your datasource
   Console.WriteLine(a);//you could display alert

Best Regards,
Wenyan Zhang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thank you for the explanation and code example, I have a question, if my view elements are updated in a service is it a bad practice or not ? Thank you @WenyanZhang-MSFT

0 Votes 0 ·

I don't think " it a bad practice", however, we need to be on the main/UI thread to load the view. The ViewModel communicates with view, the BindingContext for the View is usually an instance of the ViewModel, you could handle the data by the service.

0 Votes 0 ·

Thank's, I will do that.

0 Votes 0 ·