Lightswitch C#MessageBoxResult错误(UnautherizedAccessException:无效的跨线程访问)

Lightswitch C#MessageBoxResult错误(UnautherizedAccessException:无效的跨线程访问),c#,string,messagebox,lightswitch-2013,C#,String,Messagebox,Lightswitch 2013,我正在慢慢地开始使用Lightswitch进行一些小项目,但我遇到了一个问题,我已经查阅了一些帖子和日志,目前还没有找到解决方案。这里的任何帮助都将不胜感激 作为说明;我正在使用Visual Studio 2013 Ultimate 我遇到的错误是用户代码未处理UnauthorizedAccessException 我遇到问题的代码段粘贴在下面,用户单击按钮也会调用该段。这用于捕获用户选择的“确定”或“取消”,并根据用户的选择执行单独的操作 public void Restart_Prompt(

我正在慢慢地开始使用Lightswitch进行一些小项目,但我遇到了一个问题,我已经查阅了一些帖子和日志,目前还没有找到解决方案。这里的任何帮助都将不胜感激

作为说明;我正在使用Visual Studio 2013 Ultimate

我遇到的错误是用户代码未处理UnauthorizedAccessException

我遇到问题的代码段粘贴在下面,用户单击按钮也会调用该段。这用于捕获用户选择的“确定”或“取消”,并根据用户的选择执行单独的操作

public void Restart_Prompt()
{
    MessageBoxResult result = MessageBox.Show("Yippy", "Hello", MessageBoxButton.OKCancel);

    if (result == MessageBoxResult.OK)
    {
        MessageBox.Show("Selected option was Ok");
    }
    else
    {
        MessageBox.Show("Selected option was Cancel...");
    }
}
同样,在此问题上提供任何指导或帮助都将不胜感激

如果有人感兴趣,以下是错误的详细文本:

{System.UnauthorizedAccessException: Invalid cross-thread access.
   at MS.Internal.XcpImports.CheckThread()
   at MS.Internal.XcpImports.MessageBox_ShowCore(Window window, String messageBoxText, String caption, UInt32 type)
   at System.Windows.MessageBox.ShowCore(Window window, String messageBoxText, String caption, MessageBoxButton button)
   at System.Windows.MessageBox.Show(String messageBoxText, String caption, MessageBoxButton button)
   at LightSwitchApplication.INVENTORiesListDetail.Restart_Prompt()
   at LightSwitchApplication.INVENTORiesListDetail.Restart_ASI_Execute()
   at LightSwitchApplication.INVENTORiesListDetail.DetailsClass.MethodSetProperties._Restart_ASI_InvokeMethod(DetailsClass d, ReadOnlyCollection`1 args)
   at Microsoft.LightSwitch.Details.Framework.Internal.BusinessMethodImplementation`2.<TryInvokeMethod>b__5()
   at Microsoft.LightSwitch.Utilities.Internal.UserCodeHelper.CallUserCode(Type sourceType, String methodName, String instance, String operation, ILoggingContext context, Action action, String additionalText, Func`1 getCompletedMessage, Boolean tryHandleException, Boolean swallowException, Exception& exception)}
{System.UnauthorizedAccessException:无效的跨线程访问。
在MS.Internal.XcpImports.CheckThread()处
在MS.Internal.XcpImports.MessageBox\u ShowCore(窗口窗口,字符串MessageBox文本,字符串标题,UInt32类型)
在System.Windows.MessageBox.ShowCore(窗口窗口、字符串messageBoxText、字符串标题、MessageBoxButton按钮)
在System.Windows.MessageBox.Show(字符串MessageBox文本、字符串标题、MessageBox按钮)
在LightSwitchApplication.INVENTORiesListDetail.Restart_提示符()处
在LightSwitchApplication.INVENTORiesListDetail.Restart_ASI_Execute()上
在LightSwitchApplication.INVENTORiesListDetail.DetailsClass.MethodSetProperties.\u重新启动\u ASI\u调用方法(DetailsClass d,ReadOnlyCollection`1参数)
在Microsoft.LightSwitch.Details.Framework.Internal.BusinessMethodImplementation`2.b_u 5()
在Microsoft.LightSwitch.Utilities.Internal.UserCodeHelper.CallUserCode(类型sourceType、String methodName、String实例、String操作、ILogingContext上下文、Action操作、String附加文本、Func`1 getCompletedMessage、Boolean tryHandleException、Boolean异常、异常和异常)}

在.Net-WPF和WinForms中,UI是线程仿射的。换句话说,与UI的所有交互都必须发生在UI线程上。在WPF中,这是通过Dispatcher类型实现的,在WinForms中,这是通过每个控件上的Invoke方法实现的。正在从后台线程调用您的方法,因此您需要将调用封送到UI线程以防止出现错误。在灯光开关中,我认为是这样的:

Dispatchers.Main.BeginInvoke(()=>
{
    // message box method call here
});

添加“using Microsoft.LightSwitch.Threading”的using语句,您的解决方案对我有效。。。谢谢你的指点。