C# Error: The contextual keyword 'var' may only appear within a local variable declaration

C# Error: The contextual keyword 'var' may only appear within a local variable declaration

我编写了以下函数来创建 axWindowsMediaPlayer 播放列表:

WMPLib.IWMPPlaylist p2 = axWindowsMediaPlayer.playlistCollection.newPlaylist("Playlist 1");

private void CreatePlaylist(string _currentId)
{
  string selectedElementPageTypeValue = MainContentAreaBl.GetSelectedElementPageTypeValue(_currentId);
  var selectedElementJumpToValue = MainContentAreaBl.GetSelectedElementValue(_currentId, "jumpTo");
  if (selectedElementJumpToValue != null)
  {
     _currentId = selectedElementJumpToValue;                
     if (_currentId != null && _currentId != "menu" && selectedElementPageTypeValue == "video")
     {
        var playerFile = Path.Combine(Common.ContentFolderPath, MainContentAreaBl.GetSelectedElementDataPathValue(_currentId));
        p2.appendItem(axWindowsMediaPlayer.newMedia(playerFile));
        axWindowsMediaPlayer.currentPlaylist = p2;
        CreatePlaylist(_currentId);
     }                
     axWindowsMediaPlayer.Ctlcontrols.play();
  }
}

此处 var p2 声明为 class 级别。当我编译我的应用程序时,我收到以下错误消息:

The contextual keyword 'var' may only appear within a local variable declaration

但是,我不能将 var p2 = axWindowsMediaPlayer.playlistCollection.newPlaylist("Playlist 1"); 放入递归函数中,因为它会在每次迭代时创建新的播放列表。

如何在我的函数中访问 p2?

编辑 1: 我在输出中看到了这个 Window

COM Reference 'WMPLib' is the interop assembly for ActiveX control 'AxWMPLib' but was marked to be linked by the compiler with the /link flag. This COM reference will be treated as a reference and will not be linked.

此外,现在它在 axWindowsMediaplayer 上显示以下错误:

A field initializer cannot reference the non-static field, method or property

此信息是否与我看到的错误有关?如何解决这个问题?

您必须使用正确的类型声明它,而不是使用 var:

AxWMPLib.IWMPPlaylist p2 = axWindowsMediaPlayer.playlistCollection.newPlaylist("Playlist 1");

var 只允许在局部变量上使用,而不能在字段上使用,这就是错误消息告诉您的内容。错误消息并不意味着字段声明在错误的地方,您只是对字段类型使用了错误的语法。

MSDN 说:

To correct this error

If the variable belongs at class scope, give it an explicit type. Otherwise move it inside the method where it will be used.

所以你可以在声明变量类型的同时给出正确的类型,比如

IWMPPlaylist p2 = axWindowsMediaPlayer.playlistCollection.newPlaylist("Playlist 1");

否则您可以将变量移动到将要使用它的方法中。在您的情况下,您可以将其移动到 CreatePlaylist 方法中。

其他人已经提到了这个问题。您想要的特定类型是 IWMPPlaylist,因此整行将如下所示。

IWMPPlaylist p2 = axWindowsMediaPlayer.playlistCollection.newPlaylist("Playlist 1");

Constructor 中初始化播放列表并将播放列表作为参数传递,正如@thumbnumkeys(现已删除)所回答的那样。这是对我有用的代码:

namespace ABC
{
public partial class MainContentArea : Form
{
    private string _currentId;        

    public MainContentArea(string topicId, Menu menu)
    {
        InitializeComponent();
        _currentId = topicId;
        _menu = menu;
        WMPLib.IWMPPlaylist p2 = axWindowsMediaPlayer.playlistCollection.newPlaylist("Playlist 1");
        CreatePlaylist(_currentId, p2);
    }
private void CreatePlaylist(string _currentId, WMPLib.IWMPPlaylist p2)
{    
   var selectedElementJumpToValue = MainContentAreaBl.GetSelectedElementValue(_currentId, "jumpTo");
   string selectedElementPageTypeValue = MainContentAreaBl.GetSelectedElementPageTypeValue(selectedElementJumpToValue);            
   if (selectedElementJumpToValue != null)
   {
      _currentId = selectedElementJumpToValue;                
      if (_currentId != null && _currentId != "menu" && selectedElementPageTypeValue == "video")
      {
         var playerFile = Path.Combine(Common.ContentFolderPath, MainContentAreaBl.GetSelectedElementDataPathValue(_currentId));                    
         p2.appendItem(axWindowsMediaPlayer.newMedia(playerFile));
         axWindowsMediaPlayer.currentPlaylist = p2;
                CreatePlaylist(_currentId, p2);
      }
            //axWindowsMediaPlayer.BringToFront();
   }
        axWindowsMediaPlayer.Ctlcontrols.play();
}
}
}