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

本文关键字:一个 另一个 URI 变量 | 更新日期: 2023-09-27 17:49:29

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

有人知道我如何将mygeopotion变量类型GeoPosition传递给我的地图类的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));
        }

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

试试这样

珍宝

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());
  ....
  }

您可以使用PhoneApplicationservice在windows phone应用程序的页面之间传递数据。这里有一个关于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");
    }

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

将清楚地解释这些方法。http://nishantcop.blogspot.in/2011/08/passing-data-between-pages-in-windows.html

在我搜索另一个问题时找到了另一种方法:

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

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

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

我有一个类似的问题,我在类之间传递用户凭证,我决定使用isolatedstoragessettings类。但据我所知,随着Windows和Windows Phone代码的合并,Windows将在未来贬低这类代码。

所以,我相信微软希望你使用这个类,这样你将来就不会被一个贬值的类所困,它叫做Windows.storage。

我的情况是,如果传递用户名和密码,如果用户是高级用户,如果当应用程序启动时,如果他们已经登录。然后它会自动重新登录用户。

这里我在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();

希望你能理解我的例子,但请参考微软的文档。这个链接显示了我用来解决我的需求的一个简单示例。