如何重用PhoneNumberChooserTask 2个元素?

时间:2014-02-07 13:41:45

标签: c# windows-phone-8 windows-phone .net-4.5

目前我有一个屏幕

enter image description here

当我按下“C”按钮时,我将允许用户从他们的联系人中选择电话号码。选择后,它将显示在textbox中。我已经实现了它。

为此,我为每个按钮创建了2个PhoneNumberChooserTask对象并使用它。

现在我希望两个按钮都有一个共同的对象PhoneNumberChooserTask,并将相应的电话号码添加到其文本框中。

这是代码,我被困住了:

public partial class MainPage : PhoneApplicationPage
    {
        PhoneNumberChooserTask phNumChoseTask;
        public MainPage()
        {
            InitializeComponent();
            phNumChoseTask = new PhoneNumberChooserTask();
            phNumChoseTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask_Completed);
        }

        void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
        {
            if (e.TaskResult == TaskResult.OK)
                PersonNo1.Text = e.PhoneNumber;  //Add number to Person1 textbox

            //How I can modify to add Person2 number to textbox
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            phNumChoseTask.Show();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            MessageBox.Show("Text Chsng");
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            //I'm able to use the same object to show but not add to Person2
            phNumChoseTask.Show();
        }
    }

我可以感觉到有可能但无法找到解决方案。请分享你的想法。

2 个答案:

答案 0 :(得分:0)

我个人会用数据绑定和使用属性来清理这一吨。

如果你只是想要一个快速而肮脏的答案,尽管就是这样。

public partial class MainPage : PhoneApplicationPage
    {
        PhoneNumberChooserTask phNumChoseTask;
        bool but1Clicked = false;
        bool but2Clicked = false;
        public MainPage()
        {
            InitializeComponent();
            phNumChoseTask = new PhoneNumberChooserTask();
            phNumChoseTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask_Completed);
        }

        void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
        {
            if (e.TaskResult == TaskResult.OK){
               if(but1Clicked)
                   PersonNo1.Text = e.PhoneNumber;  //Add number to Person1 textbox
               else
                   PersonNo2.Text = e.PhoneNumber;  //Add number to Person2 textbox
            }
            //How I can modify to add Person2 number to textbox
            but1Clicked = false;
            but2Clicked = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            but1Clicked = true;
            phNumChoseTask.Show();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            MessageBox.Show("Text Chsng");
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            but2Clicked = true;
            //I'm able to use the same object to show but not add to Person2
            phNumChoseTask.Show();
        }
    }

更新

这段代码更干净

前端

    <Grid>
        <TextBox Text="{Binding tb1,Mode=TwoWay}" />
        <TextBox Text="{Binding tb2,Mode=TwoWay}" />
        <Button Click="Button_Click" Tag="1"/>
        <Button Click="Button_Click" Tag="2"/>
    </Grid>

后端

 public partial class MainPage  : PhoneApplicationPage, INotifyPropertyChanged
    {

        PhoneNumberChooserTask phNumChoseTask;
        private String _tb1;

        public String tb1
        {
            get { return _tb1; }
            set { _tb1 = value; OnPropertyChanged("tb1"); }
        }

        private String _tb2;

        public String tb2
        {
            get { return _tb2; }
            set { _tb2 = value; OnPropertyChanged("tb2"); }
        }

        private Button _ClickedBut;

        public Button ClickedBut
        {
            get { return _ClickedBut; }
            set { _ClickedBut = value; }
        }


        public MainPage()
        {
            this.DataContext = this;
            InitializeComponent();
            phNumChoseTask = new PhoneNumberChooserTask();
            phNumChoseTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask_Completed);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ClickedBut = (sender as Button);
            phNumchoseTask.Show();
        }
        void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                if (ClickedBut.Tag == "1")
                    tb1 = e.PhoneNumber;
                else if (ClickedBut.Tag == "2")
                    tb2 = e.PhoneNumber;
            }
        }
        void OnPropertyChanged(String prop)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;


    }

答案 1 :(得分:0)

如何为每个按钮附加不同的事件处理程序方法,并将重构处理逻辑附加到单个方法(在此示例中为UpdateText)以重用更多代码:

如何将Completed事件附加到EventHandler变量。然后设置EventHandler为每个按钮点击执行不同的代码:

public MainPage()
{
    InitializeComponent();
    phNumChoseTask = new PhoneNumberChooserTask();
}

private EventHandler<PhoneNumberResult> _chooserTaskHandler;

private void Button_Click(object sender, RoutedEventArgs e)
{
    //detach current handler
    phNumChoseTask.Completed -= _chooserTaskHandler;
    //set handler action
    _chooserTaskHandler += (o, result) => UpdateText(PersonNo1, result);
    //re-attach handler
    phNumChoseTask.Completed += _chooserTaskHandler;
    phNumChoseTask.Show();
}

private void Button1_Click(object sender, RoutedEventArgs e)
{
    phNumChoseTask.Completed -= _chooserTaskHandler;
    _chooserTaskHandler += (o, result) => UpdateText(PersonNo2, result);
    phNumChoseTask.Completed += _chooserTaskHandler;
    phNumChoseTask.Show();
}

private void UpdateText(TextBox textBox, PhoneNumberResult result)
{
    if (result.TaskResult == TaskResult.OK)
        textBox.Text = result.PhoneNumber;
}

请注意,此处需要分离和重新附加事件处理程序。否则,只会执行附加前设置的操作。