如何比较在XAML中创建的按钮和在C#中创建的按钮? [英] How to compare buttons created in XAML and those in C#?

查看:173
本文介绍了如何比较在XAML中创建的按钮和在C#中创建的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看方案1的图片:

http: //www.fileden.com/files/2007/2/5/737151/Sc1.png [ ^ ]



所以这里有两个按钮和Click方法在场景1的示例中使用。

 <  按钮   标记  =  Purple5   点击  =  Button_Click_1  /  >  



 <  但是ton    名称  =  P  /  >  



  private   void  Button_Click_1( object  sender,RoutedEventArgs e)
{
Button btn = sender as 按钮;
if (sender!= null
{
string s = btn.Tag.ToString();
if (gr.Children.Contains((UIElement) this .FindName(s)) )
{
gr.Children.Remove((UIElement) this .FindName(s));
btn.IsEnabled = false ;
}
}
}





让我们检查场景2:

http://www.fileden.com/files/2007/2/ 5/737151 / Sc2.png [ ^ ]



在这里,我在代码隐藏文件中创建一个按钮,如下所示。

  public   void  GetGameButtons()
{
按钮btn = 按钮();
btn.Name = P;

Grid.Children.Add(btn);
}





据我所见,我创建了一个名为P的相同按钮像以前一样,但这次不在XAML。但是带有Tag和Click方法的按钮也不像以前那样工作,问题出在哪里?在代码隐藏中创建的按钮不会被删除。

解决方案

您在代码中创建的按钮不会调用Button_Click_1,因为您没有挂钩该事件处理程序。尝试添加

btn.Click + = Button_Click_1();

到你创建按钮的区块。



警告!如果要删除按钮,任何按钮,则需要取消挂钩事件处理程序。使用 - =运算符。如果不这样做将会影响您的表现。您必须非常小心,如果您创建或删除按钮,您还可以挂钩或取消挂钩事件处理程序。


  public   void  GetGameButtons()
{
Button btn = 按钮();
btn.Name = P;
grid.RegisterName(btn.Name,btn);

Grid.Children.Add(btn);
}





添加的额外线解决了我的问题......这么简单,但不是=)


Check this picture of scenario 1:
http://www.fileden.com/files/2007/2/5/737151/Sc1.png[^]

So here the two buttons and the Click method that is being used in the example in Scenario 1.

<Button Tag="Purple5" Click="Button_Click_1"/>


<Button Name="P"/>


private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (sender != null)
            {
                string s = btn.Tag.ToString();
                if (gr.Children.Contains((UIElement)this.FindName(s)))
                {
                    gr.Children.Remove((UIElement)this.FindName(s));
                    btn.IsEnabled = false;
                }              
            }
        }



Let''s check Scenario 2:
http://www.fileden.com/files/2007/2/5/737151/Sc2.png[^]

Here, I create a button in the Code-behind file which looks like this.

public void GetGameButtons()
        {
                Button btn = new Button();
                btn.Name = "P";

                Grid.Children.Add(btn);    
        }



As far as I can see, I''ve created an identical button with a Name="P" like before but this time not in XAML. But the button with the Tag and the Click method won''t work like before either, where is the problem? The button created in Code-behind won''t be removed.

解决方案

The button you create in code is not calling the Button_Click_1 because you are not hooking that event handler. Try adding
btn.Click += Button_Click_1();
to the block where you create the button.

WARNING! If you are going to remove a button, any button, you need to unhook the event handler. with the -= operator. Failure to do so will kill your performance. You have to be very careful that if you create or remove a button you also hook or unhook the event handler(s).


public void GetGameButtons()
        {
                Button btn = new Button();
                btn.Name = "P";
                grid.RegisterName(btn.Name, btn);

                Grid.Children.Add(btn);
        }



The extra line added solved my problem......so simple, yet not =)


这篇关于如何比较在XAML中创建的按钮和在C#中创建的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆