我想在我的应用程序中使用辅助磁贴,可以将页面固定到开始菜单,当用户单击它时可以正确显示页面。
用户尝试固定到启动菜单的页面包含从上一页接收的信息;因此,如果页面被固定到启动菜单,并直接导航到它,它将不会收到要在页面上显示的所有必要信息。
要存储从一页到另一页发送的信息,我使用 PhoneApplicationService.Current.State 来存储需要发送到下一页的数据。该数据是列表框中的选定项目。
以下是从列表框中选择要存储的项目并发送到下一页的代码
产品列表.xaml.cs
namespace App_Skin_Test_Final_.All_Files.Product_Files
{
public partial class Product_List : PhoneApplicationPage
{
string pro_list_id;
public Product_List()
{
InitializeComponent();
// ===================================== Add Search application bar =====================================
// Show application bar
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.Opacity = 1.0;
ApplicationBar.IsVisible = true;
ApplicationBar.IsMenuEnabled = true;
// Search Application bar
ApplicationBarIconButton btnSearch = new ApplicationBarIconButton();
btnSearch.IconUri = new Uri("/images/Icon Application Bars/Search.png", UriKind.Relative);
btnSearch.Text = "Search";
ApplicationBar.Buttons.Add(btnSearch);
btnSearch.Click += btnSearch_Click;
}
// function Search application bar
private void btnSearch_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/All Files/Product Files/Search.xaml", UriKind.Relative));
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.TryGetValue("pro_list_id", out pro_list_id))
{
}
}
private void lst_product_Loaded(object sender, RoutedEventArgs e)
{
//MessageBox.Show(pro_cate_id);
XDocument data = XDocument.Load("All Files/Database XML/ProductsDry.xml");
var productListData = from q in data.Descendants("DryCategory")
from itemDry in q.Elements("ItemDry") // mean: itemDry in in DryCategory
where q.Attribute("DryCategoryId").Value == pro_list_id
select new ProductsDry
{
ItemDryName = itemDry.Attribute("ItemDryName").Value,
ItemDryImage = getImage(itemDry.Attribute("ItemDryImage").Value),
ItemDryId = itemDry.Attribute("ItemDryId").Value,
ItemDryIngredients = itemDry.Attribute("ItemDryIngredients").Value,
ItemDryDesc1 = itemDry.Attribute("ItemDryDesc1").Value,
ItemDryDesc2 = itemDry.Attribute("ItemDryDesc2").Value,
ItemDryUse = itemDry.Attribute("ItemDryUse").Value
// ItemDryLink=itemDry.Attribute("ItemDryLink").Value
};
lst_product.ItemsSource = productListData;
// NavigationService.GoBack();
}
private System.Windows.Media.ImageSource getImage(string p)
{
return new BitmapImage(new Uri(p, UriKind.Relative));
}
private void lst_product_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lst_product.SelectedItem != null)
{
PhoneApplicationService.Current.State["myimage"] = (lst_product.SelectedItem as ProductsDry).ItemDryImage;
PhoneApplicationService.Current.State["ItemDryIngridient"] = (lst_product.SelectedItem as ProductsDry).ItemDryIngredients;
PhoneApplicationService.Current.State["ItemDryUse"] = (lst_product.SelectedItem as ProductsDry).ItemDryUse;
PhoneApplicationService.Current.State["ItemDryDesc1"] = (lst_product.SelectedItem as ProductsDry).ItemDryDesc1;
PhoneApplicationService.Current.State["ItemDryDesc2"] = (lst_product.SelectedItem as ProductsDry).ItemDryDesc2;
PhoneApplicationService.Current.State["ItemDryUse"] = (lst_product.SelectedItem as ProductsDry).ItemDryUse;
NavigationService.Navigate(new Uri("/All Files/Product Files/Dry/Product Detail.xaml?&pro_name=" + (lst_product.SelectedItem as ProductsDry).ItemDryName + "&pro_image=" + (lst_product.SelectedItem as ProductsDry).ItemDryImage, UriKind.Relative));
}
else return;
}
private void btnGoBack_Click(object sender, RoutedEventArgs e)
{
if (this.NavigationService.CanGoBack)
{
this.NavigationService.GoBack();
}
}
}
}
以下是从上一页页面" Product List.xaml"中接收所选项目信息的页面。这是我希望用户固定他们选择的特定产品的页面。
产品详情.xaml.cs
namespace App_Skin_Test_Final_.All_Files.Product_Files
{
public partial class Product_Detail : PhoneApplicationPage
{
string pro_name;
string pro_image;
string pro_ingridient;
//string pro_link;
string pro_use;
string pro_dryDesc1;
string pro_dryDesc2;
// ImageSource image;
private readonly string SecondaryTileUriSource = "Source=SecondaryTile";
public Product_Detail()
{
InitializeComponent();
// ===================================== Add Search application bar =====================================
// Show application bar
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.Opacity = 1.0;
ApplicationBar.IsVisible = true;
ApplicationBar.IsMenuEnabled = true;
// Search Application bar
ApplicationBarIconButton btnSearch = new ApplicationBarIconButton();
btnSearch.IconUri = new Uri("/images/Icon Application Bars/Search.png", UriKind.Relative);
btnSearch.Text = "Search";
ApplicationBar.Buttons.Add(btnSearch);
btnSearch.Click += btnSearch_Click;
ApplicationBarIconButton btnPin = new ApplicationBarIconButton();
btnPin.IconUri = new Uri("/images/Icon Application Bars/pin.png", UriKind.Relative);
btnPin.Text = "Pin";
ApplicationBar.Buttons.Add(btnPin);
btnPin.Click += btnPin_Click;
}
void btnPin_Click(object sender, EventArgs e)
{
// secondary tile can be created only as the result
// of user input in an application
ShellTile tile = this.FindTile(SecondaryTileUriSource);
if(tile==null)
{
//because the UI will navigate to Start
//When a new secondary tile is created
//only one secondary tile can be created at a time
StandardTileData tileData = this.GetSecondaryTileData();
MessageBox.Show("The SecondaryTileUriSource is " + SecondaryTileUriSource);
//having a unique NavigationUri is necessary for distinguishing this tile
string tileUri = string.Concat("/All Files/Product Files/Dry/Product Detail.xmal?", SecondaryTileUriSource);
// MessageBox.Show("the uri is " + tileUri);
ShellTile.Create(new Uri(tileUri, UriKind.Relative), tileData);
// ShellTile.Create(new Uri("/All Files/Product Files/Dry/Product Detail.xaml", UriKind.Relative), tileData);
}
}
// function Search application bar
private void btnSearch_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/All Files/Product Files/Search.xaml", UriKind.Relative));
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// Check if the Secondar tile exists
ShellTile secondaryTile = this.FindTile(SecondaryTileUriSource);
if (secondaryTile != null)
{
// ***************************************************** tile.Delete();
MessageBox.Show("Secondary tile exist.");
}
if (NavigationContext.QueryString.TryGetValue("pro_name", out pro_name))
{
}
if (NavigationContext.QueryString.TryGetValue("pro_image", out pro_image))
{
}
txtbPro_Name.Text = pro_name;
BitmapImage bm = PhoneApplicationService.Current.State["myimage"] as BitmapImage;
img_Product.Source = bm;
// Ingridient Text
pro_ingridient = PhoneApplicationService.Current.State["ItemDryIngridient"] as string;
txtb_ingridient.Text = pro_ingridient;
// Find on Somaly store
//pro_link = PhoneApplicationService.Current.State["ItemDryLink"] as String;
//txtb_link = pro_link;
// How to use product
pro_use = PhoneApplicationService.Current.State["ItemDryUse"] as string;
txtb_howToUse.Text = pro_use;
// Description 1 and Description 2
pro_dryDesc1 = PhoneApplicationService.Current.State["ItemDryDesc1"] as string;
txtb_description1.Text = pro_dryDesc1;
pro_dryDesc2 = PhoneApplicationService.Current.State["ItemDryDesc2"] as string;
txtb_description2.Text = pro_dryDesc2;
// txtb_ingridient.Text = pro_image;
// image = getImage(pro_image);
// MessageBox.Show(image);
// img_Product.Source = image;
}
private ImageSource getImage(string img)
{
return new BitmapImage(new Uri(img, UriKind.Relative));
}
/*********************************************
* Create a Secondary Tile for pin application
*********************************************/
private StandardTileData GetSecondaryTileData()
{
StandardTileData titleData = new StandardTileData
{
Title = "Secondary Tile",
BackBackgroundImage = new Uri("/Images/Allures/Base/aba001.jpg", UriKind.Relative),
Count = 5,
BackTitle = "Secondary Tile",
BackgroundImage = new Uri("", UriKind.Relative),
BackContent = "WPG Add Remove Tile Sample"
};
return titleData;
}
private ShellTile FindTile(string partOfUri)
{
ShellTile shellTile = ShellTile.ActiveTiles.FirstOrDefault(
title => title.NavigationUri.ToString().Contains(partOfUri));
return shellTile;
}
}
}
当用户按下Product Detail.xaml页面上的pin应用栏按钮时,该页面将正常固定到启动菜单;但是,当他们按下启动菜单上的固定页面时,它永远不会导航回“产品详细信息”页面。但是如果我在产品detail.xaml.cs中注释掉每一行PhoneApplicationService.Current.State,它可以导航到页面,但它不会在页面上显示任何内容,即所选项目的信息。
如何更正此错误以显示用户固定为正常的页面? 请帮助。感谢
答案 0 :(得分:2)
直接问题是,从辅助磁贴启动应用时,不会填充PhoneApplicationService.Current.State字典中的值,因此您无法读取它们。
但即使你解决了这个并找出状态未填充的原因,我发现代码中存在更大的问题。我想您会显示产品列表,并可以导航到每个产品的详细信息,并希望允许用户将特定产品固定到“开始”页面。因此,假设您将所选产品存储在手机状态字典中,然后固定详细信息。您打开应用程序并选择另一个产品 - 因此它现在将存储在应用程序状态,并且点击固定的磁贴将打开第二个产品详细信息......
我的建议是重新设计页面之间的交互 - 如果您有一些可识别产品的唯一ID,将该ID传递到产品详细信息页面,并将相同的ID存储在辅助磁贴的uri中,例如这个: 新的Uri(“/所有文件/产品文件/ Dry / Product Detail.xaml?& item_id =”+ itemId)
然后,在产品详细信息页面的OnNavigated()方法中,从查询字符串中读取id,然后访问完整的产品列表,找到具有该ID的产品,并显示它。 这样,所有导航 - 从辅助磁贴,恢复应用程序等都可以正常工作。