如何将数据从 CustomMessageBox 传递到调用它的页面?

How to pass data from CustomMessageBox to the page which called it?

我有一个页面,我将一个 xaml 布局页面 称为显示国家/地区列表的 CustomMessageBox。我在 LongListSelector 的帮助下显示国家/地区列表。选择国家后,我只想关闭 CustomMessageBox 并返回所选国家到调用此 CustomMessageBox 的页面。如何做到这一点? 这是以下代码片段:

第一页:

  CountrySelectionDialog countrySelectedDialog = new CountrySelectionDialog();
  CustomMessageBox cmd = new CustomMessageBox()
  {
        Content = countrySelectedDialog,
        Opacity = 0.9
  };

第二页,即 CountrySelectionDialog 页面:

  private void on_Country_Selection(object sender, SelectionChangedEventArgs e)
    {
        if(e.AddedItems.Count>0)
        {
            country = e.AddedItems[0] as Country;
             // Now how to go back with selected country to the original page?

        }
    }

在长列表选择器的选择更改事件中使用此代码。

LLs_SelectionChange(....) 
{
String s= lls.SelectedItem;
MessageBox.close();
NavigationService.Navigate(new Uri("/YourPage.xaml?country="+s),UriKind.RelativeorAbsolute);
}

并使用查询字符串在您的页面中获取国家/地区。

使用IsolatedStorageSettings

示例

IsolatedStorageSettings userSettings=IsolatedStorageSettings.ApplicationSettings;
userSettings["Country"]=country;
userSettings.Save();

在你得到国家后添加这个,然后添加NavigationService.GoBack()和你想要国家值的地方,使用下面的代码检查国家是否存储在存储中

if(userSettings.ContainsKey("Country")
{
    country=userSettings["Country"];
}

如果 Country 是字符串,以上将起作用,如果它是对象,您可以使用 PhoneApplicationService 来保存国家值,然后返回并检查 PhoneApplicationService 是否包含 Country 对象。

检查这个:

private void on_Country_Selection(object sender, SelectionChangedEventArgs e)
    {
        if(e.AddedItems.Count>0)
        {
            country = e.AddedItems[0] as Country;
            PhoneApplicationService.Current.State("COUNTRY") = country;
               //close the page



        }
    }

现在随时随地检索国家代码:

String countrycode  = PhoneApplicationService.Current.State("COUNTRY");

我使用了以下 link How to create a LongListSelector control programmatically 而不是创建一个单独的文件作为 CustomMessageBox,我以编程方式创建了自己的 xaml 文件并在 CustomMessageBox