官术网_书友最值得收藏!

  • Xamarin Blueprints
  • Michael Williams
  • 205字
  • 2021-07-08 11:48:24

Setting up IoC with Android

Now for the IoC implementation. It works exactly the same as iOS; let's add the Android module:

    public class DroidModule : IModule 
    { 
        public void Register(ContainerBuilder builer) 
        { 
            builer.RegisterType<TextToSpeechDroid> ().As<ITextToSpeech> ().SingleInstance (); 
        } 
    } 

Easy, right?

Now we have to set up the IoC container in our MainActivity.cs class; simply copy the iOS function in the AppDelegate file called initIoC and paste this into the MainActivity class, replace the instantiation of the iOSModule with your DroidModule, then simply add the function call after the initialization of Xamarin.Forms:

protected override void OnCreate (Bundle bundle) 
        { 
            base.OnCreate (bundle); 
 
            global::Xamarin.Forms.Forms.Init (this, bundle); 
 
            InitIoC (); 
 
            LoadApplication (new App ()); 
        } 
 
        private void InitIoC() 
        { 
            IoC.CreateContainer (); 
            IoC.RegisterModule (new DroidModule()); 
            IoC.RegisterModule (new PCLModule()); 
            IoC.StartContainer (); 
        } 

Note

You may have issues trying to get the speech to work on Android. One thing you may need to set up first is within Settings | Controls | Text-to-Speech options. Here is where you will have to install voice data if the default has not already been installed. If you run the app and no speech occurs, you will have to configure the voice data.

That's all for Android, now try running the app and hear some speech.

主站蜘蛛池模板: 禹州市| 平果县| 鞍山市| 托克逊县| 古蔺县| 米脂县| 富平县| 双流县| 和平县| 宁武县| 郴州市| 板桥市| 林周县| 左云县| 忻州市| 富源县| 杭州市| 百色市| 三明市| 江城| 和林格尔县| 永仁县| 友谊县| 盐城市| 大姚县| 揭东县| 大荔县| 东宁县| 句容市| 广元市| 肃宁县| 巴楚县| 焉耆| 盱眙县| 页游| 成安县| 东阳市| 古浪县| 日土县| 明光市| 日照市|