- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
C# 相对较新,想尝试使用它来使用一些第三方 Web 服务 API。
这是XAML代码
<Grid x:Name="ContentGrid" Grid.Row="1">
<StackPanel>
<Button Content="Load Data" Click="Button_Click" />
<TextBlock x:Name="TwitterPost" Text="Here I am"/>
</StackPanel>
</Grid>
这是C#代码
private void Button_Click(object sender, RoutedEventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.twitter.com/1/users/show/keykoo.xml");
request.Method = "GET";
request.BeginGetResponse(new AsyncCallback(twitterCallback), request);
}
private void twitterCallback(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
TextReader reader = new StreamReader(response.GetResponseStream());
string strResponse = reader.ReadToEnd();
Console.WriteLine("I am done here");
TwitterPost.Text = "hello there";
}
我猜这是由于回调在与 UI 不同的线程上执行的事实造成的?在 C# 中处理这些类型的交互的正常流程是什么?
谢谢。
最佳答案
通过 CheckAccess 调用轻松进行跨线程访问的一种有用方法是将实用方法包装在静态类中 - 例如
public static class UIThread
{
private static readonly Dispatcher Dispatcher;
static UIThread()
{
// Store a reference to the current Dispatcher once per application
Dispatcher = Deployment.Current.Dispatcher;
}
/// <summary>
/// Invokes the given action on the UI thread - if the current thread is the UI thread this will just invoke the action directly on
/// the current thread so it can be safely called without the calling method being aware of which thread it is on.
/// </summary>
public static void Invoke(Action action)
{
if (Dispatcher.CheckAccess())
action.Invoke();
else
Dispatcher.BeginInvoke(action);
}
}
然后您可以像这样在后台线程中包装任何更新 UI 的调用:
private void twitterCallback(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
TextReader reader = new StreamReader(response.GetResponseStream());
string strResponse = reader.ReadToEnd();
UIThread.Invoke(() => TwitterPost.Text = "hello there");
}
这样你就不必知道你是否在后台线程上,它避免了为每个控件添加方法来检查它的开销。
关于c# - 未经授权的访问异常 : Invalid cross-thread access in Silverlight application (XAML/C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3420282/
据我所知,要将声音设置为铃声,应将其插入 MediaStore。在 MediaStore 中写入,需要 WRITE_EXTERNAL_STORAGE 权限。但是...有没有办法在不需要 WRITE_E
我只是想设置铃声。我不想授予 WRITE_SETTINGS 权限,我可以找到大部分答案来授予 WRITE_SETTINGS 权限但是我正在使用一个应用程序,该应用程序没有设置铃声的 WRITE_SET
我在 Windows 10 中以管理员身份运行 Android studio。AVD 是 Nexus 5X API 28。我正在尝试运行 flutter 演示,但设备下拉框仍然显示“无设备”,它只是有
我的应用程序构建于 spring-social-twitter允许用户使用 Twitter 登录的功能最近已停止工作。 我收到如下错误消息: Callback URL not approved for
我正在尝试使用 python-firebase 更新 Firebase库,但无法使用经过修改的示例代码进行身份验证: from firebase import firebase as fb auth
今天,当我尝试使用 GCC7 编译一个非常简单的 C++ 程序时,我遇到了一个非常奇怪的问题:程序没有向构造函数中的 vector 添加任何元素,当编译时没有优化(例如 -O0/-Og ) 来自 Re
简单问题:我正在尝试使用 Discord API 备份服务器(或公会,如果您使用官方术语)上的所有消息。 因此,我实现了 OAuth,没有任何问题,我有访问 token ,并且可以查询一些端点(我尝试
您好,我正在使用 msdn 中的以下代码供我公司内部使用: using System; public sealed class Singleton { private static volati
我们从 Google 的 GCM 服务中收到间歇性的 401 Unauthorized 错误。在过去,它 100% 的时间都有效。该问题可能与我们的路由器接受 IPv6 流量同时发生,但即使我们在适配
我有一个使用 Playwright + TS-Jest 设置 E2E 测试的项目。为了组织我的测试,我使用页面对象模型。结构看起来像这样: 我想在 tsconfig.json 中使用 TypeScri
我有一个后端应用程序在 Google Cloud Storage 中同步文件,我想在 javascript 中列出存储中的所有文件,而不需要从后端请求它们。我已经设置了 CORS,并且所有文件的 ac
我在尝试在私有(private) gitlab 存储库中发布 Artifact 时遇到问题。我正在使用 Maven 并使用个人访问 token 进行身份验证。当我运行 mvn deploy -s ~/
这是从 Google+ 登录中使用的 GoogleApiClient 获取 token 的传统方式: String token = GoogleAuthUtil.getToken(apiClient.
我在阅读 facebook Open Graph 文档后比较确定我不能让网站“订阅”公共(public)页面,除非该页面安装了我的应用程序。如果那是错误的,请告诉我。 我想做的是一个照片库,非常简单,
我是一名优秀的程序员,十分优秀!