- 使用shareCode Project分享程式碼
- 針對每一個平台撰寫程式碼
- 使用assembly-level
- 使用DenpendencyService
Xamarin 摁豆油學習筆記
2016年5月22日 星期日
如果使用Xamarin.Forms開發, 某功能開發Xamarin.Forms沒有支援該怎麼辦??
Platform-Specific Code
這個方式, 可在Layout中, 針對iOS, Android及WINPHONE不同的版型進行設定, 使Layout看起來更加的優美, 在指令上, 開頭皆是DEVICE開頭, 如:
其中Device.OnPlatform(40,20,20)即是針對(iOS, Android, Winphone)設定不同的邊距
另外還有很多指令可針對不同平台設定..
其中Device.OnPlatform(40,20,20)即是針對(iOS, Android, Winphone)設定不同的邊距
另外還有很多指令可針對不同平台設定..
2016年2月26日 星期五
checkBox
Layout :
<TextView
android:text="未選擇"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt" />
<CheckBox
android:text="香蕉"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cbBanana" />
<CheckBox
android:text="西瓜"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cbWatermelon" />
<CheckBox
android:text="鳳梨"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cbAnanas" />
<CheckBox
android:text="草莓"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cbStrawberry" />
Activity:
FindViewById<CheckBox>(Resource.Id.cbBanana)
.CheckedChange += CheckedChange;
FindViewById<CheckBox>(Resource.Id.cbWatermelon)
.CheckedChange += CheckedChange;
FindViewById<CheckBox>(Resource.Id.cbAnanas)
.CheckedChange += CheckedChange;
FindViewById<CheckBox>(Resource.Id.cbStrawberry)
.CheckedChange += CheckedChange; //checkedchange副程式在底下
//..Smart 寫法 (將上面四個FindViewById改成下述程式碼 )
//不用每個checkBox都定義,以迴圈方式自動定義,則可自由新增checkBox而不受影響
var cbLayout = FindViewById<LinearLayout>(Resource.Id.cbLayout);//定義LinearLayout裡面的所有cbLayout
int cbCount = cbLayout.ChildCount;
for (int i = 0; i < cbCount; i++)
{
var cbItem = cbLayout.GetChildAt(i);
if (cbItem is CheckBox)
{
((CheckBox)cbItem)
.CheckedChange += CheckedChange;
}
}
//..Smart 寫法
private List<string> _cacheSelect = new List<string>(); //如果checkbox被選取,值就會寫到List中
void CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
var txt =
FindViewById<TextView>(Resource.Id.txt);
var cb = ((CheckBox)sender);
if (e.IsChecked)
_cacheSelect.Add(cb.Text); //被選到的cb,其text會被加到_cacheSelect
else
_cacheSelect.Remove(cb.Text);
txt.Text = GenSelectString();
}
private string GenSelectString() //將所有被選擇的CB值集合至此處一次顯示
{
StringBuilder select = new StringBuilder();
foreach (var s in _cacheSelect)
select.AppendFormat(",{0}", s);
if (string.IsNullOrEmpty(select.ToString()))
return "未選擇";
else
return "你選擇了" + select.ToString().Substring(1);
}
<TextView
android:text="未選擇"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt" />
<CheckBox
android:text="香蕉"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cbBanana" />
<CheckBox
android:text="西瓜"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cbWatermelon" />
<CheckBox
android:text="鳳梨"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cbAnanas" />
<CheckBox
android:text="草莓"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cbStrawberry" />
Activity:
FindViewById<CheckBox>(Resource.Id.cbBanana)
.CheckedChange += CheckedChange;
FindViewById<CheckBox>(Resource.Id.cbWatermelon)
.CheckedChange += CheckedChange;
FindViewById<CheckBox>(Resource.Id.cbAnanas)
.CheckedChange += CheckedChange;
FindViewById<CheckBox>(Resource.Id.cbStrawberry)
.CheckedChange += CheckedChange; //checkedchange副程式在底下
//..Smart 寫法 (將上面四個FindViewById改成下述程式碼 )
//不用每個checkBox都定義,以迴圈方式自動定義,則可自由新增checkBox而不受影響
var cbLayout = FindViewById<LinearLayout>(Resource.Id.cbLayout);//定義LinearLayout裡面的所有cbLayout
int cbCount = cbLayout.ChildCount;
for (int i = 0; i < cbCount; i++)
{
var cbItem = cbLayout.GetChildAt(i);
if (cbItem is CheckBox)
{
((CheckBox)cbItem)
.CheckedChange += CheckedChange;
}
}
//..Smart 寫法
private List<string> _cacheSelect = new List<string>(); //如果checkbox被選取,值就會寫到List中
void CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
var txt =
FindViewById<TextView>(Resource.Id.txt);
var cb = ((CheckBox)sender);
if (e.IsChecked)
_cacheSelect.Add(cb.Text); //被選到的cb,其text會被加到_cacheSelect
else
_cacheSelect.Remove(cb.Text);
txt.Text = GenSelectString();
}
private string GenSelectString() //將所有被選擇的CB值集合至此處一次顯示
{
StringBuilder select = new StringBuilder();
foreach (var s in _cacheSelect)
select.AppendFormat(",{0}", s);
if (string.IsNullOrEmpty(select.ToString()))
return "未選擇";
else
return "你選擇了" + select.ToString().Substring(1);
}
2016年2月17日 星期三
Adapter使用方法 (以Spinner為例)
//資料陣列
(1)string[] dataSpinner = {"Visual Studio","Xamarin Studio"};
//由資料來源建立Adapter
(2)ArrayAdapter<string> sourceSpinner = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleSpinnerItem, dataSpinner);
//Layout.SimpleSpinnerItem:顯示Style
(3) spinner1.Adapter = sourceSpinner;
流程:
建立資料後,將資料匯入Adapter裡,
1,資料 ==> 2.Adapter (含Spinner顯示屬性) ==> 3.spinner
(1)string[] dataSpinner = {"Visual Studio","Xamarin Studio"};
//由資料來源建立Adapter
(2)ArrayAdapter<string> sourceSpinner = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleSpinnerItem, dataSpinner);
//Layout.SimpleSpinnerItem:顯示Style
(3) spinner1.Adapter = sourceSpinner;
流程:
建立資料後,將資料匯入Adapter裡,
1,資料 ==> 2.Adapter (含Spinner顯示屬性) ==> 3.spinner
Spinner
Values:
<resources>
<string name="ApplicationName">UserInterface1</string>
<string name="SpinnerTitle">請選擇一個值</string>//Spinner Dialog型式之Title
<string-array name="SpinnerData"> //陣列形式作為選單資料
<item>string Visual Studio</item>
<item>string Xamarin Studio</item>
</string-array>
</resources>
Layout:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" //點下拉選單後,會跳出新視窗
android:prompt="@string/SpinnerTitle" //Spinner Dialog型式之Title
android:id="@+id/spinner1" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:prompt="@string/SpinnerTitle"
android:id="@+id/spinnerR" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtSpinner1" />
註:@string/SpinnerTitle:呼叫string.xml之SpinnerTitle
Activity:
//...Spinner
string[] dataSpinner = {"Visual Studio","Xamarin Studio"};
var spinner1 = this.FindViewById<Spinner>(Resource.Id.spinner1);
var txtSpinner1 = this.FindViewById<TextView>(Resource.Id.txtSpinner1);
//由資料來源建立Adapter
ArrayAdapter<string> sourceSpinner = new ArrayAdapter<string>
(this, Android.Resource.Layout.SimpleSpinnerItem, dataSpinner);
spinner1.Adapter = sourceSpinner;
//偵測誰被選擇
spinner1.ItemSelected += (sender, e) =>
{
txtSpinner1.Text = "你選擇了" + dataSpinner[e.Position];
//被選擇的dataSpinner, 更新至txtSpinner1
};
//換比較好看的Style,也可以到Adapter設定,將SimpleSpinnerItem換成SimpleListItemSingleChoice即可,dropdown也可用
sourceSpinner.SetDropDownViewResource
(Android.Resource.Layout.SimpleListItemSingleChoice);
//由字串資源檔取回下拉選單資料
var spinnerR = this.FindViewById<Spinner>(Resource.Id.spinnerR);
ArrayAdapter sourceFromR = ArrayAdapter.CreateFromResource(
this,Resource.Array.SpinnerData,
Android.Resource.Layout.SimpleSpinnerItem);
spinnerR.Adapter = sourceFromR;
sourceFromR.SetDropDownViewResource
(Android.Resource.Layout.SimpleListItemSingleChoice);
//...Spinner
<resources>
<string name="ApplicationName">UserInterface1</string>
<string name="SpinnerTitle">請選擇一個值</string>//Spinner Dialog型式之Title
<string-array name="SpinnerData"> //陣列形式作為選單資料
<item>string Visual Studio</item>
<item>string Xamarin Studio</item>
</string-array>
</resources>
Layout:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" //點下拉選單後,會跳出新視窗
android:prompt="@string/SpinnerTitle" //Spinner Dialog型式之Title
android:id="@+id/spinner1" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:prompt="@string/SpinnerTitle"
android:id="@+id/spinnerR" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtSpinner1" />
註:@string/SpinnerTitle:呼叫string.xml之SpinnerTitle
Activity:
//...Spinner
string[] dataSpinner = {"Visual Studio","Xamarin Studio"};
var spinner1 = this.FindViewById<Spinner>(Resource.Id.spinner1);
var txtSpinner1 = this.FindViewById<TextView>(Resource.Id.txtSpinner1);
//由資料來源建立Adapter
ArrayAdapter<string> sourceSpinner = new ArrayAdapter<string>
(this, Android.Resource.Layout.SimpleSpinnerItem, dataSpinner);
spinner1.Adapter = sourceSpinner;
//偵測誰被選擇
spinner1.ItemSelected += (sender, e) =>
{
txtSpinner1.Text = "你選擇了" + dataSpinner[e.Position];
//被選擇的dataSpinner, 更新至txtSpinner1
};
//換比較好看的Style,也可以到Adapter設定,將SimpleSpinnerItem換成SimpleListItemSingleChoice即可,dropdown也可用
sourceSpinner.SetDropDownViewResource
(Android.Resource.Layout.SimpleListItemSingleChoice);
//由字串資源檔取回下拉選單資料
var spinnerR = this.FindViewById<Spinner>(Resource.Id.spinnerR);
ArrayAdapter sourceFromR = ArrayAdapter.CreateFromResource(
this,Resource.Array.SpinnerData,
Android.Resource.Layout.SimpleSpinnerItem);
spinnerR.Adapter = sourceFromR;
sourceFromR.SetDropDownViewResource
(Android.Resource.Layout.SimpleListItemSingleChoice);
//...Spinner
2016年2月16日 星期二
單選選單
Layout:RadioGroup內預設包含3個RadioButton
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioGroup1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" // 預設點選此項,如果沒有的話,就沒有點選
android:text="Visual Studio"
android:id="@+id/radioVS" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Xamarin Studio"
android:id="@+id/radioXS" />
<TextView //用來顯示誰被點選
android:text="點選上方選項"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtRadio" />
<Button
android:text="取得值"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnRadio" />
</RadioGroup>
Activity:重點在於判斷誰被點選,並將值取出
//...RadioGroup / Button
var txtRadio = this.FindViewById<TextView>(Resource.Id.txtRadio);
var radioGroup = this.FindViewById<RadioGroup>(Resource.Id.radioGroup1);
//判斷誰被點選了
radioGroup.CheckedChange += (sender, e) =>
{
int id = e.CheckedId; //被點選者資訊傳給id
var select = this.FindViewById<RadioButton>(id);//讀取被點選RadioButton資訊
txtRadio.Text = string.Format("你點選了{0}", select.Text);
//讀取select之Text屬性內容,覆寫至txtRadio之Text屬性內容
};
//另一種判斷誰被點選方式by btn
var btnRadio = this.FindViewById<Button>(Resource.Id.btnRadio);
btnRadio.Click += (sender, e) =>
{
var select = this.FindViewById<RadioButton>
(radioGroup.CheckedRadioButtonId); //重點指令
txtRadio.Text = string.Format("按鈕取得了{0}", select.Text);
};
//...RadioGroup / Button
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioGroup1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" // 預設點選此項,如果沒有的話,就沒有點選
android:text="Visual Studio"
android:id="@+id/radioVS" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Xamarin Studio"
android:id="@+id/radioXS" />
<TextView //用來顯示誰被點選
android:text="點選上方選項"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtRadio" />
<Button
android:text="取得值"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnRadio" />
</RadioGroup>
Activity:重點在於判斷誰被點選,並將值取出
//...RadioGroup / Button
var txtRadio = this.FindViewById<TextView>(Resource.Id.txtRadio);
var radioGroup = this.FindViewById<RadioGroup>(Resource.Id.radioGroup1);
//判斷誰被點選了
radioGroup.CheckedChange += (sender, e) =>
{
int id = e.CheckedId; //被點選者資訊傳給id
var select = this.FindViewById<RadioButton>(id);//讀取被點選RadioButton資訊
txtRadio.Text = string.Format("你點選了{0}", select.Text);
//讀取select之Text屬性內容,覆寫至txtRadio之Text屬性內容
};
//另一種判斷誰被點選方式by btn
var btnRadio = this.FindViewById<Button>(Resource.Id.btnRadio);
btnRadio.Click += (sender, e) =>
{
var select = this.FindViewById<RadioButton>
(radioGroup.CheckedRadioButtonId); //重點指令
txtRadio.Text = string.Format("按鈕取得了{0}", select.Text);
};
//...RadioGroup / Button
訂閱:
文章 (Atom)