Mono.Android 基础
Mono.Android 基礎 (地址)
Mono.Android項目結構是
— Project+ Assets+ Resources+ drawable+ layout+ valuesResource.Designer.csXXActivity.cs其中, Layout文件夾下存放App的前端UI文件,前端UI是一個后綴名為.axml的XML文件,該文件有兩個視圖:Design和Source。在Design視圖中支持可視化控件的拖拽。 App的后端是Activity的類,自己寫的類都要繼承基類Activity, 并在自己類中操作前端頁面的控件。 Assets文件夾下存放項目的靜態文件,例如你的大綱XML文件等,這里的文件可以通過以下流方法Assets.Open()讀取:?
using (StreamReader sr = new StreamReader(Assets.Open("sample.xml"))){string content = sr.ReadToEnd();}Resource.Designer.cs文件會記錄所有項目中的控件的Id, 也包括UI頁面。有時候在頁面上加入一個新的控件以后,它的Id并沒有自動加入Resource.Designer.cs這個文件,或者是這個文件沒有重新生成。出現這個情況,一是可以單擊保存所有 按鈕,? 然后在解決方案窗口中單擊刷新圖標, 然后,打開文件Resource.Designer.cs , 然后關閉文件Resource.Designer.cs。 如果還是不行,可以檢查項目文件(XX.csproj,使用Notepad打開), 確保以下三行存在:?
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix> <AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile> <AndroidResgenClass>Resource</AndroidResgenClass>關聯Activity的前端UI頁面
使用SetContentView(Resource.Layout.Main)將Activity類關聯到前端頁面。完成關聯以后,可以通過FindViewById()獲得頁面中定義的控件。
// Set our view from the "main" layout resourceSetContentView(Resource.Layout.Main);// Get our button from the layout resource,// and attach an event to itButton button = FindViewById<Button>(Resource.Id.MyButton); button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };Activity的特性MainLauncher=true,標識這個文件是應用的入口。
初始時代碼如下:?
using Android.App; using Android.Widget; using Android.OS; using System.IO; using System.Xml;namespace Example.Mono.Android {[Activity(Label = "Example.Mono.Android", MainLauncher = true, Icon = "@drawable/icon")]public class MainActivity : Activity{int count = 1;protected override void OnCreate(Bundle bundle){base.OnCreate(bundle);// Set our view from the "main" layout resourceSetContentView(Resource.Layout.Main);// Get our button from the layout resource,// and attach an event to itButton button = FindViewById<Button>(Resource.Id.MyButton); button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };using (StreamReader sr = new StreamReader(Assets.Open("sample.xml"))){string content = sr.ReadToEnd();XmlDocument xDoc = new XmlDocument();xDoc.LoadXml(content);var level = xDoc.SelectNodes("//SecondLevel[@id='sl1']");}}} }關于頁面跳轉
在Layout中加入新Android Layout頁面Second.axml, 在項目中加入新Activity類SecondActivity.cs。在Main頁面,單擊Button,然后跳轉到Second頁面,并且把參數傳遞過去。 創建新的Activity的實例是使用Intent,在Intent中把當前Activity的上下文傳進去,使用SecondActivity類型初始化Intent,即var secondActivity = new Intent(this, typeof(SecondActivity));。? 使用secondActivity.PutExtra()可以把參數傳到second頁, secondActivity.PutExtra("Arg1", "Argument from main page!");。啟動該Intent,StartActivity(secondActivity);。 代碼如下:
button.Click += delegate {var secondActivity = new Intent(this, typeof(SecondActivity));secondActivity.PutExtra("Arg1", "Argument from main page!");StartActivity(secondActivity);};在second頁的OnCreate方法中,使用Intent.GetStringExtra接受傳遞的參數。 代碼如下:
[Activity(Label = "SecondActivity")] public class SecondActivity : Activity {protected override void OnCreate(Bundle bundle){base.OnCreate(bundle);// Create your application hereSetContentView(Resource.Layout.Second);TextView textView1 = FindViewById<TextView>(Resource.Id.textView1);var argument = Intent.GetStringExtra("Arg1") ?? "Not Available";textView1.Text = "Welcome! It's TextView from second page." + argument;} }轉載于:https://www.cnblogs.com/qixue/p/4816918.html
總結
以上是生活随笔為你收集整理的Mono.Android 基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为啥泰山的地下水资源如此丰富?
- 下一篇: 如何保护泰山的珍稀动植物资源?