One of the keys to maintaining a responsive GUI is to do long-running tasks on a background thread so the GUI doesn't get blocked. Let's say we want to calculate a value to display to the user, but that value takes 5 seconds to calculate:

public class ThreadDemo : Activity
{
TextView textview;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create a new TextView and set it as our view
textview = new TextView (this);
textview.Text = "Working..";
SetContentView (textview);
SlowMethod ();
}
private void SlowMethod ()
{
Thread.Sleep (5000);
textview.Text = "Method Complete";
}
}

This will work, but the application will "hang" for 5 seconds while the value is calculated. During this time, the app will not respond to any user interaction. To get around this, we want to do our calculations on a background thread:

public class ThreadDemo : Activity
{
TextView textview;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create a new TextView and set it as our view
textview = new TextView (this);
textview.Text = "Working..";
SetContentView (textview);
ThreadPool.QueueUserWorkItem (o => SlowMethod ());
}
private void SlowMethod ()
{
Thread.Sleep (5000);
textview.Text = "Method Complete";
}
}

Now we calculate the value on a background thread so our GUI stays responsive during the calculation. However, when the calculation is done, our app crashes, leaving this in the log:

E/mono    (11207): EXCEPTION handling: Android.Util.AndroidRuntimeException: Exception of type 'Android.Util.AndroidRuntimeException' was thrown.
E/mono (11207):
E/mono (11207): Unhandled Exception: Android.Util.AndroidRuntimeException: Exception of type 'Android.Util.AndroidRuntimeException' was thrown.
E/mono (11207): at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms)
E/mono (11207): at Android.Widget.TextView.set_Text (IEnumerable`1 value)
E/mono (11207): at MonoDroidDebugging.Activity1.SlowMethod ()

This is because you must update the GUI from the GUI thread. Our code updates the GUI from the ThreadPool thread, causing the app to crash. We need to calculate our value on the background thread, but then do our update on the GUI thread, which is handled with Activity.RunOnUIThread:

 
public class ThreadDemo : Activity
{
TextView textview;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create a new TextView and set it as our view
textview = new TextView (this);
textview.Text = "Working..";
SetContentView (textview);
ThreadPool.QueueUserWorkItem (o => SlowMethod ());
}
private void SlowMethod ()
{
Thread.Sleep (5000);
RunOnUiThread (() => textview.Text = "Method Complete");
}
}

This code works as expected. This GUI stays responsive and gets properly updated once the calculation is comple.

Note this technique isn't just used for calculating an expensive value. It can be used for any long-running task that can be done in the background, like a web service call or downloading internet data.

获取屏幕宽度:
WindowManager.DefaultDisplay.Width

第二种方式
    var metrics = Resources.DisplayMetrics;
    var widthInDp = ConvertPixelsToDp(metrics.WidthPixels);
    var heightInDp = ConvertPixelsToDp(metrics.HeightPixels);

private int ConvertPixelsToDp(float pixelValue)
{
var dp = (int) ((pixelValue)/Resources.DisplayMetrics.Density);
return dp;
}

获取sd卡目录
Java.IO.File f = Android.OS.Environment.GetExternalStoragePublicDirectory (Android.OS.Environment.DownloadCacheDirectory);

String filePath =Environment.getExternalStorageDirectory()+"/testAudio.mp3";File mFile =newFile(filePath);

获取当前工作场景大小

//IDAndroidContent为最顶级容器的ID标识
            Android.Widget.FrameLayout flayout = FindViewById<Android.Widget.FrameLayout> (Window.IdAndroidContent);
//通过post方法,获得初始化后的flayout,并获取他的大小
            flayout.Post (delegate {                 Android.Graphics.Rect rect = new Android.Graphics.Rect();                 flayout.GetWindowVisibleDisplayFrame(rect);
         //两种方式,方式1:
                //button.Text = "w="+rect.Width()+",height="+rect.Height()+",y="+rect.Top;
         //方式2:
                button.Text="w="+flayout.Width+",h="+flayout.Height;             });

最新文章

  1. Python之路----------shutil模块
  2. MyBatis使用总结+整合Spring
  3. Yii2.0中文开发向导——控制器(Controller)
  4. 破解 RCA_CRACKME(解除隐藏按钮)
  5. C# winform DataGridView
  6. javase swing
  7. jQuery执行流程:
  8. MongoDB系列:把mongodb作为windows的服务来启动
  9. python基础——面向对象进阶
  10. Python全栈开发之---迭代器、可迭代对象、生成器
  11. VBS 备份文件
  12. E3Upload项目总结
  13. SSH(poderosa)を使って、さくらのMySQLサーバーに接続する方法
  14. Chrome 屏蔽广告
  15. webpack 搭建问题汇总
  16. c# 通过GroupBy 进行分组
  17. 自动化预备知识上&amp;&amp;下--Android自动化测试学习历程
  18. forbidden
  19. 1208. [HNOI2004]宠物收养场【平衡树-splay】
  20. has been modified since the precompiled header地图错误

热门文章

  1. CSS 学习-文本 段落
  2. Appium常用API(一)
  3. Python 操作 Excel 、txt等文件
  4. 关于.net和java des加密
  5. Windows解决多版本python执行pip3时出错AttributeError: module &#39;enum&#39; has no attribute &#39;IntFlag&#39;?
  6. C++三种野指针及应对/内存泄露
  7. 使用PHPstudy在Windows服务器下部署PHP系统
  8. elk日志分析平台安装
  9. BZOJ2430 chocolate
  10. kvm虚拟化之kvm虚拟机快照备份