C# 如何为动态创建的TextBlock提供点击(或点击)事件 public void myTextBlock1_点击(对象发送者,System.Windows.Input.GestureEventArgs e) { StackPanel mystack=newstackpanel(){Height=100,Width=200}; TextBlock myTextBlock1=新的TextBlock() {Text=“文本块”,宽度=350,高度=40,字体大小=20, 垂直对齐=垂直对齐。中心, TextAlignment=TextAlignment.Center, HorizontalAlignment=HorizontalAlignment.Center,}; mystack.Children.Add(myTextBlock1); } 对于(int r=0;r

C# 如何为动态创建的TextBlock提供点击(或点击)事件 public void myTextBlock1_点击(对象发送者,System.Windows.Input.GestureEventArgs e) { StackPanel mystack=newstackpanel(){Height=100,Width=200}; TextBlock myTextBlock1=新的TextBlock() {Text=“文本块”,宽度=350,高度=40,字体大小=20, 垂直对齐=垂直对齐。中心, TextAlignment=TextAlignment.Center, HorizontalAlignment=HorizontalAlignment.Center,}; mystack.Children.Add(myTextBlock1); } 对于(int r=0;r,c#,xaml,windows-phone-8,uwp,C#,Xaml,Windows Phone 8,Uwp,我想在创建文本块时动态触发事件。没有生成错误,但tap(或针对UWP的tap)事件不会触发该函数。公共部分类主页面:PhoneApplicationPage public void myTextBlock1_Tap(object sender, System.Windows.Input.GestureEventArgs e) { StackPanel mystack = new StackPanel() { Height = 100, Width = 200 };

我想在创建文本块时动态触发事件。没有生成错误,但tap(或针对UWP的tap)事件不会触发该函数。

公共部分类主页面:PhoneApplicationPage
public void myTextBlock1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {

        StackPanel mystack = new StackPanel() { Height = 100, Width = 200 };
        TextBlock myTextBlock1 = new TextBlock() 
            { Text = "Text Block", Width = 350, Height = 40, FontSize = 20,
              VerticalAlignment = VerticalAlignment.Center, 
              TextAlignment = TextAlignment.Center, 
              HorizontalAlignment = HorizontalAlignment.Center, };
        mystack.Children.Add(myTextBlock1);
    }


for (int r = 0; r < m; r++)
        {
            TextBlock myTextBlockr = new TextBlock() 
                { Text = "Text Block", Width = 350, Height = 40, FontSize = 20,
                  VerticalAlignment = VerticalAlignment.Center, 
                  TextAlignment = TextAlignment.Center, 
                  HorizontalAlignment = HorizontalAlignment.Center };

            if (r == 0)

            {
                myTextBlockr.Tap += new 
                   EventHandler<GestureEventArgs> (myTextBlock1_Tap);
            }
            stack1.Children.Add(myTextBlockr);
            myTextBlockr.Text = a[r];
        }
{ //建造师 公共主页() { int m=3; 初始化组件(); 对于(int r=0;r
此代码正在运行,已执行并检查了相同的代码

public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            int m = 3;
            InitializeComponent();
            for (int r = 0; r < m; r++)
            {
                TextBlock myTextBlock = new TextBlock()
                {
                    Text = "Text Block",
                    Width = 350,
                    Height = 40,
                    FontSize = 20,
                    VerticalAlignment = VerticalAlignment.Center,
                    TextAlignment = TextAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Center
                };

                //If tap event required for all text box
                myTextBlock.Tap += myTextBlock1_Tap;

                //According to your code here you have triggered tap event 
                //only for the first textblock
                if (r == 0)
                {
                    myTextBlock.Tap += new
                       EventHandler<GestureEventArgs>(myTextBlock1_Tap);
                }
                // Adding to the parent Stackpanel

                stack1.Children.Add(myTextBlock);
                myTextBlock.Text = "My textblock "+r;
            }

        }
        public void myTextBlock1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            StackPanel mystack = new StackPanel() { Height = 100, Width = 200 };
            TextBlock myTextBlock1 = new TextBlock()
            {
                Text = "Text Block",
                Width = 350,
                Height = 40,
                FontSize = 20,
                VerticalAlignment = VerticalAlignment.Center,
                TextAlignment = TextAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Center,
            };
            mystack.Children.Add(myTextBlock1);
            // Adding to the parent Stackpanel
            stack1.Children.Add(mystack);
        }
    }