为什么我不能通过 FindViewById 方法初始化 VideoView 对象?

Why can I not initialise a VideoView object by the FindViewById method?

我想播放放在文件夹 Resources/raw 中的 mp4 视频。以下是我的代码,显示错误 "A field initialiser can not be applied to the non static fiel or non static method or property" (翻译自德语), 'FindViewById(Resource.Id.vv)' 是下划线部分。在我的布局文件中,我包含了一个 ID 为 'vd' 的 VideoView。如果能播放我的视频,我将不胜感激!

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;

namespace App_1_design
{
    [Activity(Theme = "@style/AppTheme", MainLauncher = true]
    public class MainActivity : AppCompatActivity
    {
        VideoView vid = (VideoView) FindViewById(Resource.Id.vv);

我们需要在设置布局文件后在 OnCreate 事件中初始化控件:

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    VideoView vid;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        vid = FindViewById<VideoView>(Resource.Id.vv);

        var uri = Android.Net.Uri.Parse("android.resource://" + PackageName + "/" + Resource.Raw.sample_video);
        vid.SetVideoURI(uri);
        vid.Start();
    }
}

更新

why now the error message says 'resource does not contain a definition for raw'?

确保您的视频的构建操作是 AndroidResource。然后我们可以通过 Resource.Raw.[yourVideoID].

访问这个资源