降龍無悔 發表於 2019-8-29 19:37:00

C#-Xamarin的Android项目开发(一)——创建项目

<p><span style="font-size: 24px">创建项目</span></p>
<p>使用Xamarin开发安卓项目,首先需要安装VS2017以上版本。因为VS2017以上的版本,可以直接创建Xamarin项目。</p>
<p>另外用Xamarin开发安卓项目,还需要使用Intel的CPU,并且得是双核以上的CPU,因为调试时,需要使用电脑的虚拟化,奔腾4之类的CPU是不支持虚拟化的。</p>
<p>下面我们创建KibaXamarin_Android项目,如下图:</p>
<p><img id="img15670785176640" src="https://img2018.cnblogs.com/other/1762180/201908/1762180-20190829193557174-1523434510.png" alt=""></p>
<p>点击确定后,会弹出一个选择模板的窗体,这里我们选择一个空白应用,并且选择最小安卓版本号为4.4,如下图:</p>
<p><img id="img15670785176722" src="https://img2018.cnblogs.com/other/1762180/201908/1762180-20190829193557035-606296813.png" alt=""></p>
<p>&nbsp;点击OK后,项目创建完成,解决方案内容如下图</p>
<p><img id="img15670785176734" src="https://img2018.cnblogs.com/other/1762180/201908/1762180-20190829193557001-516110862.png" alt=""></p>
<p>解决方案中重要的文件及文件夹如下:</p>
<p>Resources/layout/activity_main.axml:该文件为主页面。</p>
<p>MainActivity.cs:该文件为主页面对应的后台页面,也我们进行逻辑操作或者调用逻辑操作的地方。</p>
<p>Resources/value/xxx.xml:value文件夹下主要存储常用的值,类似于我们C#中的const常量。</p>
<p>其他文件夹及文件暂时忽略。</p>
<p>在Resources文件夹里,我们可以发现,没有存储图片的地方,那么,我们创建一个文件夹drawable用来存储图片。</p>
<p>为什么用drawable存图片?答案很简单,因为网上的开源样式里的图片大多放在了drawable里,建立一个这样的文件夹,绝对会减少我们的工作量。</p>
<p>接下来,我们看一下我们的核心文件,MainActivity,代码如下:</p>
<div class="cnblogs_code">
<pre>
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MainActivity : AppCompatActivity
{
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> OnCreate(Bundle savedInstanceState)
    {
      </span><span style="color: rgba(0, 0, 255, 1)">base</span><span style="color: rgba(0, 0, 0, 1)">.OnCreate(savedInstanceState);
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Set our view from the "main" layout resource</span>
<span style="color: rgba(0, 0, 0, 1)">      SetContentView(Resource.Layout.activity_main);
    }
}</span></pre>
</div>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<p>&nbsp;首先,我们看第一行的特性,这里有三个属性赋值,含义如下:</p>
<p>Label:页面的标题。</p>
<p>Theme:页面的样式。</p>
<p>MainLauncher:是否是主窗体,该属性在项目只能给一个页面。</p>
<p>然后,我们可以看到我们的主页面MainActivity继承了AppCompatActivity,这里的AppCompatActivity是一个继承了Activity的子类,我们暂时先不了解它,因为我们即将创建一个继承Activity的BaseActivity,后续的也将继承BaseActivity。</p>
<p>接下来我们看到了OnCreate方法,这里我们需要了解下Activity的生命周期,OnCreate是Activity的第一个触发的方法,可以暂时先理解为Activity的构造函数。</p>
<p>OnCreate方法里我们看到了SetContentView(Resource.Layout.activity_main),根据字面我们先简单的理解该方法为设置内容视图。</p>
<p>可以看到我们在设置内容视图的时候,去资源里找了一个页面;也就是说,在Android中,视图是倒装的,现有Activity然后由Activity来控制要导入那个页面视图显示。</p>
<p>为了更好的寻找视图,我们将视图名和活动名进行统一,修改页面的名为MainActivity,然后再重新设置内容视图。(这里有个编译器的BUG,我们改名以后,编译器并没有同步,所以我们需要清理一下,再重新生成,如果还不成功,就删除obj文件夹,再重新生成)</p>
<p>BaseActivity</p>
<p>通过上面的描述,我们初步了解了Xamarin的Android项目。</p>
<p>现在我们一起创建一个BaseActivity。</p>
<p>首先我们需要为BaseActivity封装一些提示信息的方法,让继承该类的活动可以更简单的调用提示。</p>
<p>然后我们封装寻找资源的方法;在Android项目里是由活动调用视图,即先有活动后有视图,所以在活动里找页面的控件也是倒装的,那么这个寻找控件的方法就相对代码会比较多,所以我们简单封装一下。</p>
<p>接下来我们在封装一些跳转活动、创建服务、异步调用等基础方法;BaseActivity代码如下:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">
public class BaseActivity : Activity
{
    public void ShowActivity&lt;T&gt;() where T : Activity
    {
      Intent intent = new Intent(this, typeof(T));
      StartActivity(intent);
    }
    public void OpenService&lt;T&gt;() where T : Service
    {
      Intent intent = new Intent(this, typeof(T));
      StartService(intent);
    }

    #region各种提示信息
      public void ShowToast(string msg)
      {
            Toast.MakeText(this, msg, ToastLength.Short).Show();
      }
      
      private AlertDialog dialog;
      public AlertDialog InitDialog(string msg, Action&lt;AlertDialog&gt; comfirmCallback, Action&lt;AlertDialog&gt; cancelCallback)
      {
            AlertDialog cdialog;
            //构造器
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            //标题
            builder.SetTitle("提示");
            //图标
            //builder.SetIcon(android.R.drawable.btn_dialog);
            //内容
            builder.SetMessage(msg);
            //setPositiveButton(表示按钮的文本,表示单击按钮触发单击事件)
            builder.SetPositiveButton("OK", new EventHandler&lt;DialogClickEventArgs&gt;((s, e) =&gt;
            {
                if (comfirmCallback != null)
                {
                  comfirmCallback(dialog);
                }
            }));
            builder.SetNegativeButton("Cancel", new EventHandler&lt;DialogClickEventArgs&gt;((s, e) =&gt;
            {
                if (cancelCallback != null)
                {
                  cancelCallback(dialog);
                }
            }));
            //builder.SetNeutralButton("稍后提醒", new EventHandler&lt;DialogClickEventArgs&gt;((s, e) =&gt; { }));
            cdialog = builder.Create();//构建dialog对象

            return cdialog;
      }

      public void ShowAlert(string msg, Action&lt;AlertDialog&gt; comfirmCallback = null, Action&lt;AlertDialog&gt; cancelCallback = null)
      {
            if (comfirmCallback == null)
            {
                cancelCallback = (d) =&gt; { dialog.Dismiss(); };
            }
            if (cancelCallback == null)
            {
                cancelCallback = (d) =&gt; { dialog.Dismiss(); };
            }
            dialog = InitDialog(msg, comfirmCallback, cancelCallback);
            if (dialog != null &amp;&amp; !dialog.IsShowing)
            {
                dialog.Show();
            }
      }

      public void NotifyMessage(string message, string title = "消息")
      {
            NotificationManager manager = (NotificationManager)GetSystemService(Context.NotificationService);// 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。
            PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0,
                  new Intent(this, typeof(MainActivity)), 0);
            Notification notify1 = new Notification();
            notify1.Icon = Resource.Drawable.logo;
            notify1.TickerText = JaveString("您有新短消息,请注意查收!");
            notify1.When = DateTime.Now.ToFileTime();
            notify1.SetLatestEventInfo(this, title, message, pendingIntent);
            notify1.Number = 1;
            notify1.Flags |= NotificationFlags.AutoCancel; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
                                                         // 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示
            manager.Notify(1, notify1);
      }
      public static Java.Lang.String JaveString(string str)
      {
            return new Java.Lang.String("您有新短消息,请注意查收!");
      }
      #endregion

    #region 寻找资源
      public T FindControl&lt;T&gt;(string name) where T : View
      {
            return FindViewById&lt;T&gt;(GetCode(name));
      }

      public T FindControl&lt;T&gt;(string name, Action callBack) where T : View
      {
            View view = FindViewById&lt;T&gt;(GetCode(name));
            view.Click += (s, e) =&gt;
            {
                callBack();
            };
            return FindViewById&lt;T&gt;(GetCode(name));
      }

      public int GetCode(string name)
      {
            var R = this.Resources;
            var code = (typeof(Resource.Id)).GetFields().FirstOrDefault(f =&gt; f.Name == name).GetValue(R);
            return (int)code;
      }
      #endregion

    #region 异步调用
      public void AsyncLoad(Action action)
      {
            IAsyncResult result = action.BeginInvoke((iar) =&gt;
            {
            }, null);
      }
      public void AsyncLoad(Action action, Action callback)
      {
            IAsyncResult result = action.BeginInvoke((iar) =&gt;
            {
                this.RunOnUiThread(callback);
            }, null);
      }

      public void AsyncLoad&lt;T&gt;(Action&lt;T&gt; action, T para, Action callback)
      {
            IAsyncResult result = action.BeginInvoke(para, (iar) =&gt;
            {
                this.RunOnUiThread(callback);
            }, null);
      }
      public void RunOnUi(Action action)
      {
            ((BaseActivity)this).RunOnUiThread(() =&gt; { action(); });//回UI线程
      }
      #endregion

    #region 获取数据
      public void GetRest(string url, Action&lt;JsonValue&gt; callback)
      {
            Task.Run(() =&gt;
            {
                try
                {
                  HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
                  request.ContentType = "application/json";
                  request.Method = "GET";
                  using (WebResponse response = request.GetResponse())
                  {
                        using (Stream stream = response.GetResponseStream())
                        {
                            JsonValue jsonDoc = JsonObject.Load(stream);
                            callback(jsonDoc);
                        }
                  }
                }
                catch (Exception ex)
                {
                  Log.Debug("BaseActivity", $"Exception at GetRest" + ex.Message);
                }
            });

      }
      #endregion
}
</pre>
</div>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<p>视图MainActivity.axml</p>
<p>Android视图是有xml语法来编写的,其中一些语法定义是很奇葩,但也只能去适应,没有别的办法。比如Android里定义ID名是这样的:android:id="@+id/btn_search"。我每次看这个@+id都感觉很奇葩,哈哈。</p>
<p>Xamarin的视图和Android的视图是一样的,所以我们尽可上网找一些资源来使用。</p>
<p>我们先修改视图代码如下:</p>
<div class="cnblogs_code">
<pre>&lt;?xml version=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1.0</span><span style="color: rgba(128, 0, 0, 1)">"</span> encoding=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span>?&gt;
&lt;LinearLayout xmlns:android=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://schemas.android.com/apk/res/android</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    android:orientation</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">vertical</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    android:layout_width</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">fill_parent</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    android:layout_height</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">fill_parent</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    android:background</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">#ffffffff</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
    &lt;<span style="color: rgba(0, 0, 0, 1)">LinearLayout
      android:layout_width</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">fill_parent</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">wrap_content</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:background</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@drawable/title_background</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:paddingLeft</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">5dip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:paddingRight</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">5dip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:paddingTop</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">5dip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:paddingBottom</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">5dip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:gravity</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">center_vertical</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
      &lt;<span style="color: rgba(0, 0, 0, 1)">LinearLayout
            android:orientation</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">horizontal</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:layout_width</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">match_parent</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:layout_height</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">wrap_content</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:layout_weight</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
            &lt;<span style="color: rgba(0, 0, 0, 1)">TextView
                android:textAppearance</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">?android:textAppearanceMedium</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:layout_gravity</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">center_vertical</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:layout_width</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">wrap_content</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:layout_height</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">wrap_content</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:text</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">KibaXamarin_Android</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:textColor</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">#ffffffff</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;
            &lt;<span style="color: rgba(0, 0, 0, 1)">ImageView
                android:src</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@drawable/ic_arrow_down</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:layout_gravity</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">center_vertical</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:layout_width</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">wrap_content</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:layout_height</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">wrap_content</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:textColor</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">#ffffffff</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;
      &lt;/LinearLayout&gt;
      &lt;<span style="color: rgba(0, 0, 0, 1)">FrameLayout
            android:layout_width</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">match_parent</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:layout_height</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">wrap_content</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:layout_weight</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">5</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
            &lt;<span style="color: rgba(0, 0, 0, 1)">ImageButton
                android:src</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@drawable/toolbar_upload_photo_normal</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:layout_gravity</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">right|center_vertical</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:layout_width</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">wrap_content</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:layout_height</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">wrap_content</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
                android:background</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@drawable/btn_weight</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;
      &lt;/FrameLayout&gt;
    &lt;/LinearLayout&gt;
    &lt;<span style="color: rgba(0, 0, 0, 1)">LinearLayout
      android:gravity</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">center</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:orientation</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">horizontal</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:background</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@drawable/search_bar_background</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:padding</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">6.0dip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:layout_width</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">fill_parent</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">wrap_content</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
      &lt;<span style="color: rgba(0, 0, 0, 1)">Button
            android:textAppearance</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">?android:textAppearanceMedium</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:gravity</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">left|center</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:id</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@+id/btn_search</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:layout_width</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0.0dip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:layout_height</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">wrap_content</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:hint</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">\u0020Click Me</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:drawableLeft</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@drawable/ic_btn_search</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
            android:layout_weight</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1.0</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;
    &lt;/LinearLayout&gt;
    &lt;<span style="color: rgba(0, 0, 0, 1)">GridView
      android:paddingTop</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">20dip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:gravity</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">center</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:id</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@+id/my_grid</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:layout_width</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">fill_parent</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:layout_height</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0.0px</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:layout_marginTop</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0.0dip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:horizontalSpacing</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">10.0dip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:verticalSpacing</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">20.0dip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:stretchMode</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">columnWidth</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:columnWidth</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">60.0dip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:numColumns</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      android:layout_weight</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1.0</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      style</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">@style/CustomGridView</span><span style="color: rgba(128, 0, 0, 1)">"</span> /&gt;
&lt;/LinearLayout&gt;</pre>
</div>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>


                </tr>


        </tbody>


</table>
<p>Xamarin的简单应用</p>
<p>现在,我们的页面和BaseActivity已经完成,让我们一起做一些简单的使用把。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> OnCreate(Bundle savedInstanceState)
{
    </span><span style="color: rgba(0, 0, 255, 1)">base</span><span style="color: rgba(0, 0, 0, 1)">.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.MainActivity);
    Button btn_search </span>= <span style="color: rgba(0, 0, 255, 1)">this</span>.FindControl&lt;Button&gt;(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">btn_search</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    btn_search.Click </span>+= (s, e) =&gt;<span style="color: rgba(0, 0, 0, 1)">
    {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.ShowToast(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Click Me</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    };
}</span></pre>
</div>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>


                </tr>


        </tbody>


</table>
<p>如上代码所示,我们找到了Button-btn_search,并为他创建了单击事件;看起来还不错,代码还算简洁。</p>
<p>因为BaseActivity里寻找控件的方法里,还封装了Click方法,所以我们还可以这样使用:</p>
<div class="cnblogs_code">
<pre>Button btn_search = <span style="color: rgba(0, 0, 255, 1)">this</span>.FindControl&lt;Button&gt;(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">btn_search</span><span style="color: rgba(128, 0, 0, 1)">"</span>, () =&gt; { <span style="color: rgba(0, 0, 255, 1)">this</span>.ShowToast(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Click Me</span><span style="color: rgba(128, 0, 0, 1)">"</span>); });</pre>
</div>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>


                </tr>


        </tbody>


</table>
<p>Xamarin的调试</p>
<p>Xamarin的调试非常简单,只要配置好模拟器按F5调试就可以了,因为VS2017集成了Emulator模拟器,所以我们只要运行调试,就会自动帮我们启动模拟器。</p>
<p>模拟器是配置很简单,在工具里找到Android—Android设备管理器,如下图:</p>
<p><img id="img15670785176736" src="https://img2018.cnblogs.com/other/1762180/201908/1762180-20190829193557036-458532935.png" alt=""></p>
<p>然后做一些简单配置修改,如下图:</p>
<p><img id="img15670785176738" src="https://img2018.cnblogs.com/other/1762180/201908/1762180-20190829193557224-676472337.png" alt=""></p>
<p>模拟器配置好以后,在调试启动的选项中,就会增加这个模拟器的选项,如下图:</p>
<p><img id="img156707851767410" src="https://img2018.cnblogs.com/other/1762180/201908/1762180-20190829193556815-2043169609.png" alt=""></p>
<p>接下来就很简单了,只要直接点击运行就可以了。</p>
<p>运行结果如下图:</p>
<p><img id="img156707851767412" src="https://img2018.cnblogs.com/other/1762180/201908/1762180-20190829193557098-397532378.png" alt=""></p>
<p>从图中我们可以看到,我们的安装项目已经成功运行了,并且执行了点击事件。</p>
<p>到此,这个简单的安卓项目已经创建完成了,下一篇文章,将介绍Xamarin中如何使用安卓控件。</p>
<p>----------------------------------------------------------------------------------------------------</p>
<p>代码已经传到Github上了,欢迎大家下载。</p>
<p>Github地址:https://github.com/kiba518/KibaXamarin_Android</p>
<p>----------------------------------------------------------------------------------------------------<br>
原文连接:https://www.cnblogs.com/kiba/<br>
&nbsp;</p>
<p>欢迎添加个人微信号:Like若所思。</p>
<p>欢迎关注我的公众号,不仅为你推荐最新的博文,还有更多惊喜和资源在等着你!一起学习共同进步!</p>
<p><img id="img156628651393916" src="https://img2018.cnblogs.com/other/1762180/201908/1762180-20190829193557281-1993565679.jpg" alt=""></p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
   
<div class="div_masklayer" id="div_masklayer"></div>
<div class="div_popup" id="Div_popup"> <img class="img_zfb" id="img_zfb" src="https://images.cnblogs.com/cnblogs_com/cool2feel/1535460/o_IMG_20190827_161853.jpg">
<p class="mid">您的资助是我最大的动力!<br>金额随意,欢迎来赏!</p>

</div><br><br>
来源:https://www.cnblogs.com/cool2feel/p/11431612.html
頁: [1]
查看完整版本: C#-Xamarin的Android项目开发(一)——创建项目