【问题标题】:How to access GetSystemService(AudioService) in the UI layerUI层如何访问GetSystemService(AudioService)
【发布时间】:2019-01-31 07:33:21
【问题描述】:

我正在 Xamarin 中开发 android 应用程序。我想在 UI 层创建一个接口及其实现类。正如您在下面的代码中看到的, 我每次都会遇到错误,我使用

this.GetSystemService(AudioService);

它永远不会被识别。 请看下面的进口。请告诉我如何让它工作。

代码

 public class ImplClass :   
 InterfaceFile
 {
    public bool IsAllowed(Context ctx)
    {
        AudioManager audioMgr = 
 (AudioManager)this.GetSystemService(AudioService);
     }
    }

导入

using System;
using System.Runtime.Remoting.Contexts;
using Android.Media;
using MvvmCross.Platform;
using Android.Content.PM;

【问题讨论】:

    标签: c# android xamarin mvvmcross


    【解决方案1】:

    您可以使用“应用程序上下文”。

    示例:

    public class ImplClass : InterfaceFile
    {
        public bool IsAllowed(Context ctx)
        {
            AudioManager audioMgr = (AudioManager)Application.Context.GetSystemService(AudioService);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我正在使用它并且它有效。

      global::Android.App.Application.Context.GetSystemService
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-24
        • 2020-03-06
        • 1970-01-01
        • 2015-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多