简体   繁体   English

Windows Phone跨页面传输按钮句柄

[英]Windows phone transferring button handle across pages

Here's the Problem: 这是问题所在:

    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));

        }

    }

I need to use the toDoEdit handle (from a list box) on the EditTaskPage(windows phone page) What would be the easyest and or most efficient way of doing this. 我需要使用EditTaskPage(Windows电话页面)上的toDoEdit句柄(从列表框),这将是最简单或最有效的方法。 Please be specific. 请具体说明。 I am working with the windows phone local database for the first time. 我是第一次使用Windows Phone本地数据库。

Here is what my ToDoItem looks like: 这是我的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 : 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
  }


 }

There is more than one way to do this, here are two: 有多种方法可以做到这一点,这里有两种:

  1. (Quick 'n' dirty): put the ToDoItem in a well-known object such as the App object. (快速'n'弄脏):将ToDoItem放入知名对象(例如App对象)中。
  2. Put the Id of the ToDoItem in the Uri: NavigationService.Navigate(new Uri("/EditTaskPage.xaml?id=" + toDoEdit.Id , UriKind.Relative)); 将ToDoItem的ID放入Uri中: NavigationService.Navigate(new Uri("/EditTaskPage.xaml?id=" + toDoEdit.Id , UriKind.Relative)); and retrieve the item in the EditTaskPage. 并在EditTaskPage中检索该项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM