1

我在我的应用程序中使用了以下类型的导航。我需要var page在另一个班级。因为我使用了受保护的类,所以我不能调用这个 var 页面。无论如何都可以调用这个var页面。因为,我需要这个 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)
        {
        \\...
        }
    }
4

2 回答 2

1

我建议您将其保留在 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));
    }
}

在第二节课中你使用它

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/en-us/library/windowsphone/develop/jj714090%28v=vs.105%29.aspx

不要忘记用双引号关闭“.html”字符串

这个其他答案也可能有帮助

普通人应该如何在 Windows Phone 8 应用程序中保留设置?

于 2013-08-28T13:45:34.760 回答
0

根据定义,受保护的类(或成员)只能在它们自己的类或派生类中访问。

创建另一个继承受保护的类,给它一个公共属性并在构造函数中将“page”的值分配给该属性。

于 2013-08-28T13:47:51.287 回答