Windows Phone 不能在 C# 中的静态类中声明实例成员

本文关键字:静态类 声明 实例 成员 Windows 不能 Phone | 更新日期: 2023-09-27 18:37:03

嗨,我是Windows phone development新手,我做一些练习来学习如何发展。我尝试使用 ContactManager 类来获取设备的联系人,但收到此错误:

无法在静态类"联系人管理器"中声明实例成员

我的类不是static,那么为什么它仍然显示此错误?

    using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Contacts;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App2
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            mytextblock.Text = "Hi";
        }
        public void GetContacts()
        {
            ContactManager theContactManager = new ContactManager();
            foreach (Contact theContact in theContactManager.GetContactCollection())
            {
                string theLine = theContact.Names[0].FormattedName;
                foreach (PhoneNumber theNumber in theContact.PhoneNumbers)
                    theLine += "'t" + theNumber.ToString();
                listBox1.Items.Add(theLine);
                //Console.WriteLine(theLine); //Uncomment this if on console
            }
        }
    }
}

Windows Phone 不能在 C# 中的静态类中声明实例成员

是的,它是静态的:public static class ContactManager .

单击"C#"以查看声明:https://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.contacts.contactmanager.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1