Windows电话跨页面传输按钮手柄

时间:2012-02-04 07:25:34

标签: c# windows-phone-7 handle local-database

以下是问题:

    private void editTaskButton_Click(object sender, RoutedEventArgs e)
    {
        // Cast the parameter as a button.
        var button = sender as Button;

        if (button != null)
        {

            // Get a handle for the to-do item bound to the button.
            ToDoItem toDoEdit = button.DataContext as ToDoItem;

           // I need to get toDoEdit handle to the EditTaskPage so that I can work on it
            NavigationService.Navigate(new Uri("/EditTaskPage.xaml",          UriKind.Relative));

        }

    }

我需要在EditTaskPage(windows phone page)上使用toDoEdit句柄(从列表框中) 这样做最简单,最有效的方法是什么?请具体说明。我是第一次使用Windows Phone本地数据库。

这是我的ToDoItem的样子:

   public class ToDoDataContext : DataContext
  {
    // Pass the connection string to the base class.
    public ToDoDataContext(string connectionString)
        : base(connectionString)
    { }

    // Specify a table for the to-do items.
    public Table<ToDoItem> Items;

    // Specify a table for the categories.
    public Table<ToDoCategory> Categories;
}

ToDoDataContext.cs:

   using System;
   using System.ComponentModel;
   using System.Data.Linq;
   using System.Data.Linq.Mapping;

   namespace project
   {


public class ToDoDataContext : DataContext
{
    // Pass the connection string to the base class.
    public ToDoDataContext(string connectionString)
        : base(connectionString)
    { }

    // Specify a table for the to-do items.
    public Table<ToDoItem> Items;

    // Specify a table for the categories.
    public Table<ToDoCategory> Categories;
}

[Table]
public class ToDoItem : INotifyPropertyChanged, INotifyPropertyChanging
{



    // Define ID: private field, public property, and database column.
    private int _toDoItemId;

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]


    public int ToDoItemId
    {
        get { return _toDoItemId; }
        set
        {
            if (_toDoItemId != value)
            {
                NotifyPropertyChanging("ToDoItemId");
                _toDoItemId = value;
                NotifyPropertyChanged("ToDoItemId");
            }
        }
    }

    // Define item name: private field, public property, and database column.
    private string _itemName;

    [Column]
    public string ItemName
    {
        get { return _itemName; }
        set
        {
            if (_itemName != value)
            {
                NotifyPropertyChanging("ItemName");
                _itemName = value;
                NotifyPropertyChanged("ItemName");
            }
        }
    }

    // Define completion value: private field, public property, and database column.
    private bool _isComplete;

    [Column]
    public bool IsComplete
    {
        get { return _isComplete; }
        set
        {
            if (_isComplete != value)
            {
                NotifyPropertyChanging("IsComplete");
                _isComplete = value;
                NotifyPropertyChanged("IsComplete");
            }
        }
    }

    // Version column aids update performance.
    [Column(IsVersion = true)]
    private Binary _version;


    // Internal column for the associated ToDoCategory ID value
    [Column]
    internal int _categoryId;

    // Entity reference, to identify the ToDoCategory "storage" table
    private EntityRef<ToDoCategory> _category;

    // Association, to describe the relationship between this key and that "storage" table
    [Association(Storage = "_category", ThisKey = "_categoryId", OtherKey = "Id", IsForeignKey = true)]
    public ToDoCategory Category
    {
        get { return _category.Entity; }
        set
        {
            NotifyPropertyChanging("Category");
            _category.Entity = value;

            if (value != null)
            {
                _categoryId = value.Id;
            }

            NotifyPropertyChanging("Category");
        }
    }


    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    // Used to notify that a property changed
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

    #region INotifyPropertyChanging Members

    public event PropertyChangingEventHandler PropertyChanging;

    // Used to notify that a property is about to change
    private void NotifyPropertyChanging(string propertyName)
    {
        if (PropertyChanging != null)
        {
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

    #endregion
}


[Table]
public class ToDoCategory : INotifyPropertyChanged, INotifyPropertyChanging
{

    // Define ID: private field, public property, and database column.
    private int _id;

    [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
    public int Id
    {
        get { return _id; }
        set
        {
            NotifyPropertyChanging("Id");
            _id = value;
            NotifyPropertyChanged("Id");
        }
    }

    // Define category name: private field, public property, and database column.
    private string _name;

    [Column]
    public string Name
    {
        get { return _name; }
        set
        {
            NotifyPropertyChanging("Name");
            _name = value;
            NotifyPropertyChanged("Name");
        }
    }

    // Version column aids update performance.
    [Column(IsVersion = true)]
    private Binary _version;

    // Define the entity set for the collection side of the relationship.
    private EntitySet<ToDoItem> _todos;

    [Association(Storage = "_todos", OtherKey = "_categoryId", ThisKey = "Id")]
    public EntitySet<ToDoItem> ToDos
    {
        get { return this._todos; }
        set { this._todos.Assign(value); }
    }


    // Assign handlers for the add and remove operations, respectively.
    public ToDoCategory()
    {
        _todos = new EntitySet<ToDoItem>(
            new Action<ToDoItem>(this.attach_ToDo),
            new Action<ToDoItem>(this.detach_ToDo)
            );
    }

    // Called during an add operation
    private void attach_ToDo(ToDoItem toDo)
    {
        NotifyPropertyChanging("ToDoItem");
        toDo.Category = this;
    }

    // Called during a remove operation
    private void detach_ToDo(ToDoItem toDo)
    {
        NotifyPropertyChanging("ToDoItem");
        toDo.Category = null;
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    // Used to notify that a property changed
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

    #region INotifyPropertyChanging Members

    public event PropertyChangingEventHandler PropertyChanging;

    // Used to notify that a property is about to change
    private void NotifyPropertyChanging(string propertyName)
    {
        if (PropertyChanging != null)
        {
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

    #endregion
  }


 }

1 个答案:

答案 0 :(得分:1)

有多种方法可以做到这一点,这里有两个:

  1. (Quick'n'dirty):将ToDoItem放在一个众所周知的对象中,例如App对象。
  2. 将ToDoItem的ID放在Uri中:NavigationService.Navigate(new Uri("/EditTaskPage.xaml?id=" + toDoEdit.Id , UriKind.Relative));并检索EditTaskPage中的项目。