从页面调用用户控件中的函数 [英] Calling function in usercontrol from page
问题描述
大家好,
我有一个小问题...
场景:
1.我在Silverlight(SL)中有一个导航应用程序
2.我在主页上有一个名为MainMenu的菜单控件(只是简单的按钮).
3.单击按钮(在菜单中)登录"时,SL应用程序将通过导航面板导航到该页面.
4.登录时,菜单必须更改(例如,登录按钮应变为注销).
在这一点上,我不知道如何让用户控件MainMenu知道需要更改按钮.
我试图看一下,但我不知道在哪里看以及用什么关键字.
非常基本:如何从页面的主页上的用户控件中调用函数?
有人可以帮忙吗?
谢谢!
Hi all,
I have a small problem...
Scenario:
1. I have a navigation application in Silverlight (SL)
2. I have a usercontrol with the menu (just simple buttons) called MainMenu on the mainpage.
3. When I click on the button (in the menu) "Login", the SL app navigates to that page by a navigationpanel.
4. When I login the menu has to change (button Login should become Logout for example).
At this point I have no clue how I can let the usercontrol MainMenu know the button needs to change.
I tried to look, but I have no idea where to look and on what keywords.
So very basic: How can I call a function in a usercontrol on the mainpage from a page?
Can anybody help?
Thanks!
推荐答案
我不会尝试查找或跟踪控件(在将来的代码版本中可能会更改)并在运行时使用代码对其进行修改,使用以数据为中心的方法-为此而制作了WPF/Silverlight.
从Visual Studio项目模板生成业务应用程序示例...检出LoginStatus.xaml/LoginStatus.xaml.cs文件,以查看通过绑定和可视状态执行此操作的另一种方法.
基本上,有一个单例/静态对象代表登录/注销状态.此类具有用于登录和注销的事件.您需要知道状态何时更改的任何元素(如用户控件)都可以订阅这些事件,并在事件处理程序中适当地设置其状态(例如将登录"按钮更改为注销").这样可以将代码放在usercontrol的类中,而不必从其他页面/控件中跟踪或查找usercontrol.
例如,如果您将身份验证代码包装在像这样的单例类中,
Instead of trying to find or keep track of controls (which may change in future versions of your code) and modifying them with code at runtime, I would go with a more data-centric method - WPF/Silverlight are made for this.
Generate a business application sample from the visual studio project templates...check out the LoginStatus.xaml/LoginStatus.xaml.cs files to see another way of doing this with bindings and visual states.
Basically, there''s a singleton/static object that represents the logged-in/logged-out state. This class has events for loggedin and loggedout. Any element (like a usercontrol) you have that needs to know when the state changes can subscribe to these events and in the event handlers set its state appropriately (like changing a "log in" button to "log out"). This puts the code right in the usercontrol''s class instead of having to track or find usercontrols from other pages/controls.
For example, if you wrapped your authentication code in a singleton class like this
public class AuthenticationService : INotifyPropertyChanged
{
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion
private static volatile AuthenticationService instance;
private static object syncRoot = new Object();
private AuthenticationService()
{
}
public static AuthenticationService Current
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new AuthenticationService();
}
}
return instance;
}
}
public event EventHandler<EventArgs> LoggedIn;
public event EventHandler<EventArgs> LoggedOut;
public void LogIn()
{
//if and when loginsuccessful
//{
IsLoggedIn = true;
EventHandler<EventArgs> evnt = LoggedIn;
if (evnt != null)
{
evnt(this, new EventArgs());
}
//}
}
public void LogOut()
{
//if and when logoutsuccessful
//{
IsLoggedIn = false;
EventHandler<EventArgs> evnt = LoggedOut;
if (evnt != null)
{
evnt(this, new EventArgs());
}
//}
}
bool _isLoggedIn = false;
public bool IsLoggedIn
{
get { return _isLoggedIn; }
set
{
if (_isLoggedIn != value)
{
_isLoggedIn = value;
OnPropertyChanged("IsLoggedIn");
}
}
}
}
...然后需要根据登录/注销状态进行更新的用户控件可以简单地执行以下操作
...then a usercontrol that needs to update itself based on the loggedin/loggedout status can simply do something like this
public MyUserControl()
{
InitializeComponent();
// Subscribe to loggedin/loggedout events so we can update our UI
AuthenticationService authentication = AuthenticationService.Current;
authentication.LoggedIn += new EventHandler<EventArgs>(authentication_LoggedIn);
authentication.LoggedOut += new EventHandler<EventArgs>(authentication_LoggedOut);
}
void authentication_LoggedIn(object sender, EventArgs e)
{
// Change button text to "Log Out"
}
void authentication_LoggedOut(object sender, EventArgs e)
{
// Change button text to "Log In"
}
不需要其他页面来了解MyUserControl:)
No need for another page to know about MyUserControl :)
Humm,很多代码...在应用程序中找不到我需要的答案的方式... A请给我一点提示吗?也许我需要一些代码?请知道,我不需要此功能仅用于登录/注销.该应用程序的其他部分具有相似的功能/需求.
谢谢
Humm, alot of code... Can''t really find my way in the application to the answer I need... A little hint please? Perhaps some code I need? Please know I don''t need this functionality for login/-out only. In other parts of the app are similar functions/needs.
Thanks
这篇关于从页面调用用户控件中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!