如何在URI中将变量从一个类传递到另一个类?

时间:2014-03-10 23:58:37

标签: c# windows-phone-8 geocode here-api

这是我到目前为止调用GetCoordinates方法并通过单击按钮导航到地图的方法。我想知道如何传递坐标数据。

有谁知道如何将GeoPosition类型的MyGeoPosition变量传递给我的地图类的OnNavigatedTo方法?我知道如何从另一个类调用方法,但不知道如何传递变量等数据。

private async Task GetCoordinates(string name = "My Car")
        {
            await Task.Run(async () =>
            {
                // Get the phone's current location.
                Geolocator MyGeolocator = new Geolocator();
                //need to pass the below variable containing coordinate data..
                MyGeolocator.DesiredAccuracyInMeters = 5;
                Geoposition MyGeoPosition = null;
                try
                {
                    MyGeoPosition = await MyGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10));

                }
                catch (UnauthorizedAccessException)
                {
                    MessageBox.Show("Location is disabled in phone settings or capabilities are not checked.");
                }
                catch (Exception ex)
                {
                    // Something else happened while acquiring the location.
                    MessageBox.Show(ex.Message);
                }
            });
        }


        //sets location of parking space using the GetCoordinates method
        //opens map 
        private async void setLocationBtn_Click(object sender, RoutedEventArgs e)
        { 
            await this.GetCoordinates();
            NavigationService.Navigate(new Uri("/Maps.xaml", UriKind.Relative));
        }

6 个答案:

答案 0 :(得分:1)

您可以使用PhoneApplicationservice在Windows Phone应用程序中的页面之间传递数据。 Here是关于PhoneApplicationservice的好例子。以下是PhoneApplicationService如何工作的简短示例,这可能会对您有所帮助。

private async void setLocationBtn_Click(object sender, RoutedEventArgs e)
   { 
     await this.GetCoordinates();
    PhoneApplicationService.Current.State["Data"] = your data;
     NavigationService.Navigate(new Uri("/Maps.xaml", UriKind.Relative));
    }

//On Second page

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {
     var data =PhoneApplicationService.Current.State["Data"] as Cast your type
     PhoneApplicationService.Current.State.Remove("Data");
    }

答案 1 :(得分:1)

尝试这样的事情


第一页

this.NavigationService.Navigate(new Uri(string.Format("LocationView.xaml?GeoX={0}&GeoY={1}", GeoX, GeoY), UriKind.Relative));  

secondPage

  if (NavigationContext.QueryString.ContainsKey("GeoX") && NavigationContext.QueryString.ContainsKey("GeoY"))
  {
  double GeoX =Convert.ToDouble(NavigationContext.QueryString["GeoX"].ToString());
  double GeoY = Convert.ToDouble(NavigationContext.QueryString["GeoY"].ToString());
  ....
  }

答案 2 :(得分:1)

您可以通过以下四种方式传递数据,这在以下文章

中有明确说明

http://nishantcop.blogspot.in/2011/08/passing-data-between-pages-in-windows.html

答案 3 :(得分:1)

在我寻找另一个问题时找到另一种方式:

http://msdn.microsoft.com/en-us/library/windows/apps/hh771188.aspx

向下滚动至:在页面之间传递信息

它比我上面的解决方案简单得多,但我的解决方案有其他要求因此我选择那个,但是为了你的需要,这是一个更好的方法。

答案 4 :(得分:0)

我有一个类似的问题,我在类之间传递用户凭据,我决定使用IsolatedStorageSettings类。但我已经读过,Windows将来会在合并Windows和Windows Phone代码时对这个类进行折旧。

所以,this is the class我相信微软希望你能够使用,以便将来不会遇到折旧的课程,而且它被称为Windows.storage.

希望这有帮助。

答案 5 :(得分:0)

我的情况是如果用于传递用户名和密码以及用户是否为高级用户以及应用程序何时启动(如果他们已经登录)的话。然后它会自动重新登录用户。

这里我在MainPage类中创建存储

IsolatedStorageSettings myUserSettings = IsolatedStorageSettings.ApplicationSettings;

这是MainPage类方法:

 private void GetUserData()
        {
           // System.Diagnostics.Debug.WriteLine("Grabbing Data");

            if (IsolatedStorageSettings.ApplicationSettings.Contains("userLoggedIn"))
            {
                string isLoggedIn = IsolatedStorageSettings.ApplicationSettings["userLoggedIn"] as string;
                if (isLoggedIn.EndsWith("rue"))
                    isLoggedOn = true;
                else
                    isLoggedOn = false;
            //    System.Diagnostics.Debug.WriteLine("log in data " + isLoggedIn + " " + isLoggedOn);
            }
            else
            {
                myUserSettings.Add("userLoggedIn", "false");
                isLoggedOn = false;
            }



            if (IsolatedStorageSettings.ApplicationSettings.Contains("fullAccess"))
            {
                string hasFullAccess = IsolatedStorageSettings.ApplicationSettings["fullAccess"] as string;
                if (hasFullAccess.EndsWith("rue"))
                    fullAccess = true;
                else
                    fullAccess = false;
            }
            else
            {
                myUserSettings.Add("fullAccess", "false");
                fullAccess = false;
            }


            if (IsolatedStorageSettings.ApplicationSettings.Contains("username"))
            {
                username = IsolatedStorageSettings.ApplicationSettings["username"] as string;
            }
            else
            {
                myUserSettings.Add("username", "");
                username = "me";
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains("password"))
            {
                password = IsolatedStorageSettings.ApplicationSettings["password"] as string;
            }
            else
            {
                myUserSettings.Add("password", "");
                password = "v";
            }
            myUserSettings.Save();


        }

现在在我的登录类中,我必须再次创建存储变量

IsolatedStorageSettings myUserSettings = IsolatedStorageSettings.ApplicationSettings; 

现在,一旦我对用户进行了验证,我就会将相关信息写入存储文件:(部分方法缺失为无关紧要)

       // Here I have just finished using JSON to extra info from a JSON response
    if (success.EndsWith("rue"))
    {
        if (!myUserSettings.Contains("userLoggedIn"))
        {
            myUserSettings.Add("userLoggedIn", success);
        }
        else
        {
            myUserSettings["userLoggedIn"] = success;
        }


        if (!myUserSettings.Contains("username"))
        {
            myUserSettings.Add("username", username);
        }
        else
        {
            myUserSettings["username"] = username;
        }

        if (!myUserSettings.Contains("password"))
        {
            myUserSettings.Add("password", password);
        }
        else
        {
            myUserSettings["password"] = password;
        }

        if (!myUserSettings.Contains("fullAccess"))
        {
            myUserSettings.Add("fullAccess", fullAccess);
        }
        else
        {
            myUserSettings["fullAccess"] = fullAccess;
        }
        myUserSettings.Save();

如果某些内容不起作用,请检查您是否按如下方式保存文件:

       myUserSettings.Save();

希望你能理解我的榜样,但请参阅微软的doco。这个link显示了一个我用来解决我的要求的简单例子。