Windows 为不同线程编组的存储事件

Windows store event Marshalled for a different thread

我看到了类似的帖子,但是在使用 advised dispatcher 时我仍然无法摆脱这个错误,我的代码如下:

Xaml:

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

和视图模型:

        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); 
            }
        }

问题是由线路引起的:await Server.CheckUserName(UserName);

我试过了:`

    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); 
});

            }
        }`

还有其他

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

await ValidateUserName(e)));

但问题依然存在。有人可以帮我吗?

你应该像这样使用 CoreDispatcher 而不是 CoreWindow。

public CoreDispatcher dispatcher { get; set; }
   this.dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
   await this.dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                {
await Server.CheckUserName(UserName); 
});

问题已解决 - 发现收到的回复中的信使有问题