[英]c#: Accessing Protected Class Variable From Outside Class?
我在應用程序中使用了以下導航類型。 我需要在另一個班級的var page
。 由於我使用了受保護的類,因此無法調用此var頁面。 無論如何有沒有叫這個變種頁面。 因為,我需要這個變量來初始化另一個類。 那么,如何從類外部訪問受保護的類變量?
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("Page"))
{
var page = NavigationContext.QueryString["Page"];
browser.Navigate(new Uri("/f" + page + ".html, UriKind.Relative));
}
}
我這節課需要
private void def(object sender, EventArgs e)
{
switch(page)
{
\\...
}
}
我建議您將其保留在Windows Phone設置鍵值存儲中。
在頭等艙中存儲它
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("Page"))
{
var page = NavigationContext.QueryString["Page"];
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
//store it in the settings
if (!settings.Contains("qsPage"))
{
//if setting has not been created, add it
settings.Add("qsPage", page);
}
else
{
//store a the page in the setting
settings["qsPage"] = page;
}
browser.Navigate(new Uri("/f" + page + ".html", UriKind.Relative));
}
}
在seccond類中使用它
private void def(object sender, EventArgs e)
{
//if you need to check that the setting exists use this
//if (IsolatedStorageSettings.ApplicationSettings.Contains("qsPage"))
//retrieve tha value from the settings
var page = IsolatedStorageSettings.ApplicationSettings["qsPage"];
switch(page)
{
\\...
}
}
從此處的示例改編的代碼快速入門:在Windows Phone中使用設置 http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/jj714090%28v=vs.105%29.aspx
並且不要忘記關閉雙引號“ .html”字符串
這另一個答案可能也有幫助
根據定義,受保護的類(或成員)只能在其自己的類或派生類中訪問。
創建另一個繼承受保護的類的類,為其提供一個公共屬性,並在構造函數中將“ page”的值分配給該屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.