'MainActivity' 不包含 'SetVolumeControl' 的定义

'MainActivity' does not contain a definition for 'SetVolumeControl'

我正在尝试让 phone 的音量按钮在我的应用程序中自动更改媒体音量而不是铃声音量。我发现你应该把这一行 this.SetVolumeControlStream(Android.Media.Stream.Music); 我试图在 OnCreate 方法中这样做,但它说 MainActivity 不包含 SetVolumeControlStream 的定义,也没有扩展方法。我意识到我可能在错误的地方写了这一行,关于如何使这一行在我的应用程序中工作有什么建议吗?

using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Support.V7.App;
using Android.Widget;
using foosballv2s.Source.Activities.Adapters;
using foosballv2s.Source.Activities.Helpers;
using foosballv2s.Source.Entities;
using foosballv2s.Source.Services.FileIO;
using foosballv2s.Source.Services.FoosballWebService.Repository;
using Java.Interop;
using Xamarin.Forms;
using View = Android.Views.View;
using Android.Media;

namespace foosballv2s.Source.Activities
{
    /// <summary>
    /// Main activity for choosing the teams for the game
    /// </summary>
    [Activity(
        ConfigurationChanges = ConfigChanges.Orientation,
        ScreenOrientation = ScreenOrientation.Portrait
        )]
    public class MainActivity : AppCompatActivity
    {
        private AutoCompleteTextView firstTeamTextView, secondTeamTextView;
        private IO instance = new IO();
        private Game game;
        private TeamRepository teamRepository;


        protected override void OnCreate(Bundle savedInstanceState)
        {
            AudioManager audio = (AudioManager)GetSystemService(Context.AudioService);
            int currentVolume = audio.GetStreamVolume(Android.Media.Stream.Music);
            this.SetVolumeControlStream(Android.Media.Stream.Music);
            base.OnCreate(savedInstanceState);

            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

            SetContentView(Resource.Layout.Main);

            game = DependencyService.Get<Game>();
            teamRepository = DependencyService.Get<TeamRepository>();

            firstTeamTextView = (AutoCompleteTextView)FindViewById<AutoCompleteTextView>(Resource.Id.team1Name);
            secondTeamTextView = (AutoCompleteTextView)FindViewById<AutoCompleteTextView>(Resource.Id.team2Name);

            firstTeamTextView.ItemClick += AutoCompleteTextView_ItemClicked;
            secondTeamTextView.ItemClick += AutoCompleteTextView_ItemClicked;

            var btnP = FindViewById<Android.Widget.Button>(Resource.Id.prev);
            btnP.Click += BtnPrev_Click;

            //Window.SetBackgroundDrawable(Android.Resource.Id.);
            NavigationHelper.SetupNavigationListener(this);
            NavigationHelper.SetActionBarNavigationText(this, Resource.String.app_name);
        }

在 Xamarin 中,Java Set* 和 Get* 方法通常映射到 C# 属性。所以 SetVolumeControlStream() 变成了 VolumeControlStream 而 getter 和 setter 代替了。

这在 API 指南

中有记载
[get: Android.Runtime.Register("getVolumeControlStream", "()I", "GetGetVolumeControlStreamHandler")]
[set: Android.Runtime.Register("setVolumeControlStream", "(I)V", "GetSetVolumeControlStream_IHandler")]
public Stream VolumeControlStream { get; set; }