Search code examples
c#multithreadingwindows-store-appsmvvm-lightwin-universal-app

Windows store event Marshalled for a different thread


I saw similar posts to this however while using advised dispatcher still i can't get rid of this error, my code is as follows:

Xaml:

            <interactivity:Interaction.Behaviors>
            <core:EventTriggerBehavior EventName="LostFocus">
                <core:InvokeCommandAction Command="{Binding ValidateUserNameCommand}"
                   InputConverterParameter="{Binding ElementName=UserNameTextBox}" />
            </core:EventTriggerBehavior>
        </interactivity:Interaction.Behaviors>

And ViewModel:

        public RelayCommand<RoutedEventArgs> ValidateUserNameCommand
    { get; private set; }

    public ChooseUsernameViewModel(IServerService _server, INavigationServiceCustom _nav) 
    {
        Server = _server;
        Navigation = _nav;

        ValidateUserNameCommand = new RelayCommand<RoutedEventArgs>(async (e) => await ValidateUserName(e));
    }    

        private async Task ValidateUserName(RoutedEventArgs eArgs)
        {
            if (String.IsNullOrEmpty(UserName) || String.IsNullOrWhiteSpace(UserName))
            {
            }
            else
            {

                await Server.CheckUserName(UserName); 
            }
        }

The problem is caused by line: await Server.CheckUserName(UserName);

I tried:`

    private async Task ValidateUserName(RoutedEventArgs eArgs)
        {
            if (String.IsNullOrEmpty(UserName) || String.IsNullOrWhiteSpace(UserName))
            {
            }
            else
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async () =>
{
          await Server.CheckUserName(UserName); 
});

            }
        }`

and olso

            ValidateUserNameCommand = new RelayCommand<RoutedEventArgs>(async (e) => await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async() =>

await ValidateUserName(e)));

but the problem persists. Could somebody help me?


Solution

  • Problem solved - found that messenger in received response was at fault