i get always on Xamarin with UWP the following Error:
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
I want to use:
Xamarin.Forms.BindingBase.EnableCollectionSynchronization(MyList, null, ObservableCollectionCallback);
I won't use:
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => MyList.Add(DateTime.Now.ToLongTimeString()));
I have my reasons.
Here is the very simple c# ViewModel Code. You have to bind MyList in a Xamarin Project with UWP in MainPage to a ListView:
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
MyList = new ObservableCollection<string>();
Xamarin.Forms.BindingBase.EnableCollectionSynchronization(MyList, null, ObservableCollectionCallback);
Timer timer = new Timer(new TimerCallback(Addtext));
timer.Change(3000, 1000);
}
void ObservableCollectionCallback(IEnumerable collection, object context, Action accessMethod, bool writeAccess)
{
lock (collection)
{
accessMethod?.Invoke();
}
}
public void Addtext(object state)
{
MyList.Add(DateTime.Now.ToLongTimeString());
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private ObservableCollection<string> MyListValue;
public ObservableCollection<string> MyList
{
get { return MyListValue; }
set
{
MyListValue = value;
OnPropertyChanged();
}
}
is anybody here who know, what i have to do?
In Xamarin BindingBase.EnableCollectionSynchronization doesn't Work
The problem is that you invoke Addtext
in the Timer
. And the timer triggered in the un-uithread. If you don't want to use BeginInvokeOnMainThread
, you could use Device.StartTimer
to replace Timer
.
Device.StartTimer(TimeSpan.FromSeconds(1),()=> {
Addtext(this);
return true;
} );
And please check support list, EnableCollectionSynchronization could not support UWP plafrom.
User contributions licensed under CC BY-SA 3.0