Refresh or update content page every few seconds automatically - c#

I am using Xamarin.forms (PCL) and I need to refresh/update Content Page with its data every few seconds. The data is retrieved from API in the viewmodel.
Is there any method or handler that can be used periodically to call the Get Api periodically inside the page.xaml.cs, something like:
methodRunPeriodically()
{
userdata = await UserService.GetUserasync(_UserViewModel.EmployeeId);
}

Xamarin.Forms has an API for starting a timer that you might find useful for this, documented here.
Device.StartTimer (TimeSpan.FromSeconds(10), () => {
// If you want to update UI, make sure its on the on the main thread.
// Otherwise, you can remove the BeginInvokeOnMainThread
Device.BeginInvokeOnMainThread(() => methodRunPeriodically());
return shouldRunAgain;
});

Based on the code in the above question, you would ensure that:
Your userdata object implements IPropertyChange as follows:
//Other usings skipped for brevity
...
...
using System.ComponentModel;
using System.Runtime.CompilerServices;
// This is a simple user class that
// implements the IPropertyChange interface.
public class DemoUser : INotifyPropertyChanged
{
// These fields hold the values for the public properties.
private string userName = string.Empty;
private string phoneNumber = string.Empty;
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public DemoUser()
{
}
public string Id { get; set; }
public string UserName
{
get
{
return this.userName;
}
set
{
if (value != this.userName)
{
this.userName = value;
NotifyPropertyChanged();
}
}
}
public string PhoneNumber
{
get
{
return this.phoneNumber;
}
set
{
if (value != this.phoneNumber)
{
this.phoneNumber = value;
NotifyPropertyChanged();
}
}
}
}
In your ContentPage, you then try the following, (I slightly modified the code by others above):
public class UserPage : ContentPage
{
private DemoUser demoUser;
private int intervalInSeconds;
public UserPage()
{
//Assuming this is a XAML Page....
InitializeComponent();
}
public UserPage(DemoUser demoUser, int intervalInSeconds = 10) : this()
{
this.demoUser = demoUser;
this.intervalInSeconds = intervalInSeconds;
this.BindingContext = this.demoUser;
Device.StartTimer(TimeSpan.FromSeconds(this.intervalInSeconds), () =>
{
Device.BeginInvokeOnMainThread(() => refreshDemoUser());
return true;
});
}
private async void refreshDemoUser()
{
this.demoUser = await getDemoUserById(this.demoUser.Id);
}
}

You can do as follows to run a Task when 10 seconds has passed. Returning true in Device.StartTimer will ensure that the Timer keeps running. Also, you want to ensure that you invoke the method on the main thread to update the UI:
public MyConstructor()
{
StartTimer();
}
private void StartTimer()
{
Device.StartTimer(System.TimeSpan.FromSeconds(10), () =>
{
Device.BeginInvokeOnMainThread(UpdateUserDataAsync);
return true;
});
}
private async void UpdateUserDataAsync()
{
userdata = await UserService.GetUserasync(_UserViewModel.EmployeeId);
}
If your API doesn't expose an EventHandler that you can subscribe to, then you need to do as mentioned in my example above.

You should just bind the UI to properties in your ViewModel and then set those properties appropriately. Calling OnPropertyChanged() will trigger Xamarin.Forms to update the UI based on the bound properties. Something like below:
//Code in Page
public class MyPage : ContentPage
{
public MyPage()
{
var entry = new Entry();
BindingContext = new MyViewModel();
entry.SetBinding<MyViewModel>(Entry.TextProperty, vm=>vm.EntryText);
Content = entry;
}
}
//Code in ViewModel
public class MyViewModel() : INotifyPropertyChanged
{
public MyViewModel()
{
Task.Factory.StartNew(()=> methodRunPeriodically());
}
string entryText;
public string EntryText
{
get { return entryText; }
set
{
if(entryText == value)
return;
entryText = value;
OnPropertyChanged();
}
}
bool shouldRun = true;
async Task methodRunPeriodically()
{
while(shouldRun)
{
userdata = await UserService.GetUserasync(_UserViewModel.EmployeeId);
EntryText = userdata.FirstName;
await Task.Delay(5000); //Run this every 5 seconds
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
In this pattern, we are kicking off a long-running task that will run in a loop. It is reaching out to refresh the userData every 5 seconds and then setting the EntryText property. In the setter of the EntryText property in our ViewModel, we are calling OnPropertyChanged() which will cause Xamarin.Forms to update the UI. Calling OnPropertyChanged() triggers Xamarin.Forms to switch thread context from the background task to the UI thread and then back to the background task.
I didn't write this in XAML, but the binding would be pretty much the same except the entry would be like below:
<Entry Text={Binding EntryText}/>
EDIT
#therealjohn's answer is good also. You could use that instead of my while loop like below:
bool shouldRun = true;
methodRunPeriodically()
{
Device.StartTimer(TimeSpan.FromSeconds(5), () =>
{
userdata = await UserService.GetUserasync(_UserViewModel.EmployeeId);
EntryText = userdata.FirstName;
return shouldRun;
});
}
You can review what the Forms source code is doing with the Device.StartTimer on the native iOS and Android.

Update UI every one second:
Device.StartTimer(TimeSpan.FromMilliseconds(1000), loop2);
bool loop2()
{
Device.BeginInvokeOnMainThread(() => updateUI());
return true;
}
or:
Device.StartTimer(TimeSpan.FromMilliseconds(1000), loop2);
bool loop2()
{
Device.BeginInvokeOnMainThread(() => {
updateUI();
//more stuff;
});
return true;
}

Related

Something is wrong with Dispatcher. RunAsync () [duplicate]

In a WinUI 3 in Desktop app I have a property to update which is bound to the ui via x:Bind.
I want to use the Dispatcher like I do in WPF to get on the UI thread and avoid the thread error im getting when I update the prop:
System.Runtime.InteropServices.COMException: 'The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))'
Im just not sure how to do it in WinUI 3, when I try
DispatcherQueue.GetForCurrentThread().TryEnqueue(() =>
{
AeParty.OnSyncHub = false; // Prop bound in ui using x:Bind
});
I get this error
DispatcherQueue.GetForCurrentThread() is null
I also tried:
this.DispatcherQueue.TryEnqueue(() =>
{
AeParty.OnSyncHub = false;
});
but it wont compile:
I then found this GitHub issue, so I tried:
SynchronizationContext.Current.Post((o) =>
{
AeParty.OnSyncHub = false;
}, null);
This works but why can't I get onto the UI thread with the Dispatcher in my VM?
DispatcherQueue.GetForCurrentThread() only returns a DispatcherQueue when being called on a thread that actually has a DispatcherQueue. If you call it on a background thread there is indeed no DispatcherQueue to be returned.
So the trick is to call the method on the UI thread and store the return value in a variable that you then use from the background thread, e.g.:
public sealed partial class MainWindow : YourBaseClass
{
public MainWindow()
{
this.InitializeComponent();
}
public ViewModel ViewModel { get; } = new ViewModel();
}
public class ViewModel : INotifyPropertyChanged
{
private readonly DispatcherQueue _dispatcherQueue = DispatcherQueue.GetForCurrentThread();
public ViewModel()
{
Task.Run(() =>
{
for (int i = 0; i < 10; i++)
{
string val = i.ToString();
_dispatcherQueue.TryEnqueue(() =>
{
Text = val;
});
Thread.Sleep(2000);
}
});
}
private string _text;
public string Text
{
get { return _text; }
set { _text = value; NotifyPropertyChanged(nameof(Text)); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

How can I stop a Device.StartTimer() when I change pages?

I have a ListView that I am updating every 5 seconds using Device.StartTimer() and I would like to stop the timer when it leaves the ViewModel page. as you must intuit necsito do this because Device.StartTimer () is global and even when I change the page is still updating my ListView, how can I make ViewModel know that I'm changing pages?
This is part of my ViewModel:
private ObservableCollection sensors;
public ObservableCollection<PcData> Sensors
{
get { return sensors; }
set
{
sensors = value;
OnPropertyChanged();
}
}
public MonitoringTabsViewModel(string idCode, string description)
{
Description = description;
LoadSensors(idCode);
Device.StartTimer(TimeSpan.FromSeconds(5), () =>
{
RefreshSensors(idCode);
return true;
});
}
private async void LoadSensors(string idCode)
{
Sensors = new ObservableCollection<PcData>(await App.WebApiManager.GetCurrentStatusDeviceAsync(idCode));
}
private async void RefreshSensors(string idCode)
{
Sensors = null;
Sensors = new ObservableCollection<PcData>(await App.WebApiManager.GetCurrentStatusDeviceAsync(idCode));
}
In the end I have come to the following implementation which actually does what I wanted:
ViewModel:
public class MonitoringTabsViewModel : Notificable
{
public string IdCode { get; set; }
public bool InPage { get; set; }
private string description;
public string Description
{
get { return description; }
set
{
description = value;
OnPropertyChanged();
}
}
private ObservableCollection<PcData> sensors;
public ObservableCollection<PcData> Sensors
{
get { return sensors; }
set
{
sensors = value;
OnPropertyChanged();
}
}
public MonitoringTabsViewModel(string idCode, string description)
{
IdCode = idCode;
Description = description;
LoadSensors(idCode);
MessagingCenter.Subscribe<MonitoringView>(this, "OnAppearing", (sender) =>
{
InPage = true;
});
MessagingCenter.Subscribe<MonitoringView>(this, "OnDisAppearing", (sender) =>
{
InPage = false;
});
Device.StartTimer(TimeSpan.FromSeconds(5), TimerCallBack);
}
private bool TimerCallBack()
{
if (InPage)
{
RefreshSensors(IdCode);
MessagingCenter.Unsubscribe<MonitoringView>(this, "OnAppearing");
return true;
}
else
{
MessagingCenter.Unsubscribe<MonitoringView>(this, "OnDisAppearing");
return false;
}
}
private async void LoadSensors(string idCode)
{
Sensors = new ObservableCollection<PcData>(await App.WebApiManager.GetCurrentStatusDeviceAsync(idCode));
}
private async void RefreshSensors(string idCode)
{
Sensors = null;
Sensors = new ObservableCollection<PcData>(await App.WebApiManager.GetCurrentStatusDeviceAsync(idCode));
}
}
View:
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Send<MonitoringView>(this, "OnAppearing");
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Send<MonitoringView>(this, "OnDisAppearing");
}
There are still two things that concern me:
1. I do not know if the management I'm giving to the MessagingCenter is appropriate, as you can see I'm unsubscribing in my TimerCallBack method, by putting breakpoints in the two calls to the unsubscribe method I see that while the timer is running every 5 seconds The unsubscribe method of the onAppearing message is still called.
2. Although this implmentacion works, I still have the problem that when sleeping the application or put it in the background is still running my method RefreshSensors () and I would like to be in segudno flat also stop the execution.
Could someone give me ideas of these two concerns that I still have?
Page has 2 indicator methods OnAppearing() & OnDisappearing() depends on your setup you should hookup to this events and notify the ViewModel.
This can be done in multiple ways:
Page may have a direct or indirect reference (BindingContext) to the ViewModel so just hookup.
You can use MessagingCenter.
If you have a custom handmade NavigationService you could hookup there.
Use existing MVVM Framework, there are plenty of them and most of them support this scenario
I still have the problem that when sleeping the application or put it
in the background is still running my method RefreshSensors ()
If you look in you App.xaml.cs file, you'll find the following methods:
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}

WPF Binding between pages / Transfer data between view models

I need to transfer data between pages and do "binding" on it.
On the first page i have textbox bound to "Username" property.
Every page has its own viewmodel, after clicking a button in the first page i've done something like this
SecondPageViewModel.Username = this.Username;
In second page i have textblock bound to Username property, but after page changes, the second page show no text.
<TextBlock Text="{Binding Username}" />
The only way i found and works is to in the second page viewmodel in the constructor make a task which updates the username.
Task.Run(async () =>
{
while(true)
{
await Task.Delay(200);
this.Username = FirstPageViewModel.Username;
}
});
Is there any other way to do that? By making task here, it isn't always working, sometimes if i change page too fast, it won't show username anyway.
Every viewmodel implements INotifyPropertyChanged + FodyWeaver.
Following my comment, here's some simple implementations using events.
A first implementation is FirstPageViewModel is parent of SecondPageViewModel. You can see the event subscription in the SecondPageViewModel constructor.
A second implementation is FirstPageViewModel is on the same level of SecondPageViewModel. This uses a Mediator between the two ViewModels. It is basically removing the dependency of FirstPageViewModel from SecondPageViewModel
A third one would be to create your own delegate on FirstPageViewModel for SecondPageViewModel to subscribe on. It's basically the same thing as PropertyChanged, but you can configure what event arguments you are ready to pass.
Here's a demo:
public delegate void UsernameChangedEventHandler(string username);
public class FirstPageViewModel : INotifyPropertyChanged
{
// 3) Third implementation
public event UsernameChangedEventHandler UsernameChanged;
private string _username;
public string UserName
{
get { return _username; }
set
{
_username = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("UserName"));
if (UsernameChanged != null)
UsernameChanged(this.UserName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class SecondPageViewModel : INotifyPropertyChanged
{
private string _username;
public string UserName
{
get { return _username; }
set
{
_username = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("UserName"));
}
}
public SecondPageViewModel()
{
}
public SecondPageViewModel(FirstPageViewModel parent)
{
// 1) First implementation
parent.PropertyChanged += FirstPageViewModel_OnPropertyChanged;
// 3) Third Implementation
parent.UsernameChanged += Parent_UsernameChanged;
}
private void Parent_UsernameChanged(string username)
{
this.UserName = username;
}
private void FirstPageViewModel_OnPropertyChanged(object sender, PropertyChangedEventArgs args)
{
FirstPageViewModel parent = (FirstPageViewModel) sender;
if(args.PropertyName.Equals("username", StringComparison.InvariantCultureIgnoreCase))
{
this.UserName = parent.UserName;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class ParentViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private FirstPageViewModel _firstPageViewModel;
private SecondPageViewModel _secondPageViewModel;
public ParentViewModel()
{
// 2) Second implementation
_firstPageViewModel = new FirstPageViewModel();
_secondPageViewModel = new SecondPageViewModel();
_firstPageViewModel.PropertyChanged += FirstPageViewModel_PropertyChanged;
// 3) Third Implementation
_firstPageViewModel.UsernameChanged += FirstPageViewModel_UsernameChanged;
}
private void FirstPageViewModel_UsernameChanged(string username)
{
_secondPageViewModel.UserName = username;
}
private void FirstPageViewModel_PropertyChanged(object sender, PropertyChangedEventArgs args)
{
FirstPageViewModel firstPageViewModel = (FirstPageViewModel)sender;
if (args.PropertyName.Equals("username", StringComparison.InvariantCultureIgnoreCase))
{
_secondPageViewModel.UserName = firstPageViewModel.UserName;
}
}
}

Update ViewModel property from model MVVM UWP

I am developing a UWP application where i am following MVVM pattern.
I have a property in the View Model which is bind to the view. I have one function in the service which process multiple tasks.
After each execution of activity i need to update the property which is in the View Model.
ViewModel.cs
public Brush CurrentGetExecutionColor
{
get { return _currentGetExecutionColor; }
set { Set(ref _currentGetExecutionColor, value); }
}
public DelegateCommand DelegateCommandProcess
=> _delegateCommandProcess ?? (_delegateCommandProcess = new DelegateCommand(async () =>
{
await _service.ProcessMethod();
}));
Service.cs
private async Task<bool> ProcessMethod()
{
While(condition)
{
Process();
//UpdateViewModel property
CurrentGetExecutionColor = Color.Red;
}
}
How i can achieve this functionality so that i can update View Model property from service.
Thanks in Advance.
Try to implement in your property OnPropertyChanged, like this:
private Type _yourProperty;
public Type YourProperty
{
get { return _yourProperty; }
set
{
_yourProperty = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

Binding Label to a static string

I have made a Base Form which is inherited by most Forms in the application. Base form contains a Status Bar Control that displays user name which is internally a static string. User can Switch User at any point in the application by pressing a button on status bar. At this point the user name in the status bar should also change, as if now it only changes in code and UI has no idea about the change. I have googled around and found that i need to bind the label with that static string by implementing a INotifyProperty Interface. I have implemented many example code without success.
Appreciate any help
use BindableAttribute for the property you want to bind a control to it.
[Bindable(true)]
public int Username {
get {
// Insert code here.
return 0;
}
set {
// Insert code here.
}
}
You must implement a class to notify prop changed and therefore the prop can not be static. Combine with a singleton pattern and you have yout solution.
public class Global : INotifyPropertyChanged
{
private string _userName;
public string UserName
{
get
{
return this._userName;
}
set
{
if (this._userName == value)
{
return;
}
this._userName = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("UserName"));
}
{
}
public event PropertyChangedEventHandler PropertyChanged;
private Global() {}
public static readonly Global Get = new Global();
}
Usage:
var currUserName = Global.Get.UserName;
Global.Get.PropertyChanged += (s, e) => Console.WriteLine(e.PropertyName);
Global.Get.UserName = "John";
And bind to Global.Get to property UserName.
I would:
1- Add a timer to the base form to update the status bar. (the timer resolution is uo to your requirement).
the timer Tick handler would be something like this:
private void timerStatusUpdate_Tick(object sender, EventArgs e)
{
toolStripStatusLabelMessage.Text = StatusMessage();
}
2 - Add a virtual StatusMessage method to your base class:
class BaseForm : Form
{
.......
public virtual string StatusMessage()
{
return "override me!";
}
}
3- override StatusMessage in all your derived classes
class XXXForm : BaseForm
{
........
public override string StatusMessage()
{
return "XXXForm status message";
}
}
I use Reactive Extensions for these things
For example if you have a Context class with a property UserName
you could do this
public static class Context
{
public static Subject<string> UserChanged = new Subject<string>();
private static string user;
public static string User
{
get { return user; }
set
{
if (user != value)
{
user = value;
UserChanged.OnNext(user);
}
}
}
}
And then on your forms just do
Context.UserChanged.ObserveOn(SynchronizationContext.Current)
.Subscribe(user => label.Text = user);
The ObserveOn(SynchronizationContext.Current) makes it safe for cross thread operation calls

Categories