![](/img/trans.png)
[英]how to send a javascript Object from one Client to Another by SignalR
[英]How to send object from one Frame to another
使用VS 2013,C#,Windows Store App
我需要將一個對象從主框架發送到新對象,然后使用它。 所以我有主框架,第二個框架(用於發送對象)和DataModel。
想法-是顯示我在主框架中擁有的所有數據,而不是選擇一個對象,然后在其上按,在出現新框架后,您將可以在新框架中使用選定的項目。
問題-如何將對象從一幀發送到另一幀。
目前我下一步:創建具有靜態屬性的其他靜態類:
public static class GetCurrentEvent
{
public static Event CurrentEvent { get; set; }
}
因此,首先,我在主框架上調用此類的屬性,並使用它保存所需的對象:
private void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
var clickedItems = (Event)e.ClickedItem;
GetCurrentEvent.CurrentEvent = new Event(
clickedItems.UniqueId,
clickedItems.Name,
clickedItems.Place,
clickedItems.Description,
clickedItems.Start,
clickedItems.End,
clickedItems.ImagePath
);
if (this.Frame != null)
{
this.Frame.Navigate(typeof(ChangeEvent));
}
}
之后,我在新框架中使用此屬性:
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
this.DataContext = GetCurrentEvent.CurrentEvent;
...
}
所有的作品,但我認為這不是完美的方法。
那么,有一個問題是我如何更改代碼以改進方法,或者如何將對象從一個類發送到另一個類?
編輯
選擇varinat將對象從一個幀發送到另一個幀-使用參數:
var clickedItems = (Event)e.ClickedItem;
this.Frame.Navigate(typeof(ChangeEvent), clickedItems);
然后在新框架中轉換為所需的類型:
this.DataContext = (Event)e.NavigationParameter;
實現這一目標的方法有很多種,這有爭議。
我通常選擇簡單的解決方案,例如將狀態/會話變量保存在全局可訪問的單例中。 我將單例稱為Global,並將其保留在名稱空間的根目錄中。 例:
public sealed class Global
{
#region Singlton Contructor
Global() { }
static readonly Global instance = new Global();
public static Global Default
{
get { return instance; }
}
#endregion
#region Global Settings
public Settings Settings {get;set;}
private AuthenticatedUser _authenticatedUser;
public AuthenticatedUser AuthenticatedUser
{
get
{
return _authenticatedUser;
}
set { _authenticatedUser = value; }
}
private UserSession _currentSession;
public UserSession CurrentSession
{
get
{
if (_currentSession == null) _currentSession = UserSessionService.UserSessionFactoy();
return _currentSession;
}
private set { _currentSession = value; }
}
#endregion
}
在這種情況下,CurrentSession跟蹤我要逐幀傳遞的對象。 並通過使用輕松訪問
Global.CurrentSession.SomePropertyOrObject
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.