我知道有很多与此问题有关的话题,但我没有找到自己的案例。

我收到一个错误“使用未分配的局部变量'flags'”

public class Flag : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private string _Tag;
    public string Tag
    {
        get { return _Tag; }
        set
        {
            _Tag = value;
            NotifyPropertyChanged("Tag");
        }
    }

    private string _Name;
    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            NotifyPropertyChanged("Name");
        }
    }

    private void NotifyPropertyChanged(string propertyName)
    {
        if (null != PropertyChanged)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    const string filename = "Flags.xml";
    public void Save()
    {
        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream stream = storage.CreateFile(filename);
        XmlSerializer xml = new XmlSerializer(GetType());
        xml.Serialize(stream, this);
        stream.Close();
        stream.Dispose();
    }
}


我尝试分配标志时代码主体中的问题

   public partial class MainPage : PhoneApplicationPage
    {

        public MainPage()
        {
            InitializeComponent();

            Flag flags;
            flags.Name = "1111"; //I here error
            flags.Tag = "1";     //I
                                 //I
            flags.Save();        //I
        }
    }

最佳答案

您需要初始化flags

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        Flag flags = new Flag();
        flags.Name = "1111";
        flags.Tag = "1";
        flags.Save();
    }
}

09-20 15:41