[英]Adding button to the user control using class method in Windows Phone 7
I am developing a user control for WP7 application. 我正在开发WP7应用程序的用户控件。 I added the control to
MainPage.xaml
and I want the control to create the button from a class called MyButton using the method Add_button_internal()
, but I can't to figure out how to do it. 我将控件添加到
MainPage.xaml
并且希望该控件使用Add_button_internal()
方法从名为MyButton的类中创建按钮,但我不知道该怎么做。
When I try to use a method Add_button_external();
当我尝试使用方法
Add_button_external();
which is outside the class MyButton
, there is no problem. 在
MyButton
类之外,没有问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace PhoneApp4
{
public partial class WindowsPhoneControl1 : UserControl
{
public WindowsPhoneControl1()
{
InitializeComponent();
// THIS WORKS FINE
LayoutRoot.Children.Add(Add_button_external("button name"));
// THIS DOESNT WORK :(
MyButton t = new MyButton();
t.Add_button_internal("button name");
}
public Button Add_button_external(string m)
{
Button btn = new Button();
btn.Content = m;
return btn;
}
public class MyButton
{
public MyButton() { }
public Button Add_button_internal(string n)
{
Button btn = new Button();
btn.Content = n;
return btn;
}
}
}
}
Can you please help me to solve my problem? 您能帮我解决我的问题吗?
Your MyButton classes Add_Button_Internal method only returns a newly created button, you would have to add it to the layoutroot.childer collection to display it on the UI. MyButton类的Add_Button_Internal方法仅返回一个新创建的按钮,您必须将其添加到layoutroot.childer集合中才能在UI上显示它。 So i think your problem is only that you forget to add the new button to the UI.
所以我认为您的问题仅仅是您忘记将新按钮添加到UI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.