我正在使用Silverlight 5編寫的遺留應用程序,該應用程序包含很多反模式和不良做法。我負責使用SingalR添加實時交互(如通知)。
順便說一句,他們使用WCF RIA Services these與認證進行交互。如何從另一個UserControl訪問UserControl的DataContext?
他們有一個主頁,這個頁面中,我得到用戶的通知,並顯示他們的登錄用戶名:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
//...
}
}
因此,大家可以看到我沒有設置DataContext屬性爲只要用戶登錄,我需要在應用程序用戶登錄後設置的MainPage的DataContext的,所以我必須做的是,在LoginOperation_Completed裏面LoginForm
頁:
public partial class LoginForm : StackPanel
{
private LoginRegistrationWindow parentWindow;
private LoginInfo loginInfo = new LoginInfo();
public LoginForm()
{
InitializeComponent();
//...
}
private void LoginOperation_Completed(LoginOperation loginOperation)
{
if (loginOperation.LoginSuccess)
{
// Here I need to access MainPages's DataContext property and set it with my ViewModel
}
}
}
現在我的問題是,我該怎麼設置MainPage的DataContext屬性裏面另一類(在這種情況下爲LoginFrom
)?
我也試圖給出一個ID我的MainPage用戶控制和訪問它像這樣:
mainPage.DataContext = new NotificationItemViewModel();
但是,編譯器給了我這個錯誤:
The name 'mainPage' does not exist in the current context
請添加您的XAML – Gromy
[這裏](https://gist.github.com/SirwanAfifi/c329af5029db2a8e3f51)中的XAML的MainPage。 –