Windows Phone 8 secondary tiles crash when not in Debug Mode - c#

I am testing a basic secondary live tile(ShellTile) functionality with the code below. Creating the tile works fine but using the tile to navigate to the URI always works in debug mode but not when testing disconnected from the computer and I don't know why. I am testing with just 1 tile. Funny thing is, if I restart, secondary tile will work one more time after the restart. What am I missing?
1> This is the code behind that makes the secondary tile
string path = #"/Views/test.xaml?text=" + parameter.ToString();
StandardTileData tileData = new StandardTileData
{
Title = parameter.ToString(),
BackTitle = parameter.ToString(),
BackContent = parameter.ToString()
};
ShellTile.Create(new Uri(path, UriKind.Relative), tileData);
2.test.xaml code
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string text= "";
text= NavigationContext.QueryString["text"];
}
Thanks,
Jamie

When the Tile which NavigationUri is same with your parameter, the ShellTile.Create method will crash. You should check if the tile is exist first, like this:
ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(parameter));
You can choose Contains or Equals by your content. and then:
if (TileToFind != null)
{
// Tile is existed, you can update it, but not add the same uri tile
}
else
{
// you can add new tile
}

Related

Windows 8 phone, multi tab browser not updating current url

I am in the process of making a multi tab browser and have run into some problems. I would like it so when the user has added a new tab using a button, that every site they then choose to navigate to will update to the tab url. So if the user changes tab or opens a new one, it the last website will be saved if they return to the previous tab.
Here is the code while creating a new tab & When a tab is selected from the created ones
private void addNewTab(string url)
{
TabEntry urlObj = new TabEntry();
urlObj.URL = url;
urlObj.timestamp = DateTime.Now.ToString("HH:mm");
if (url.Contains("/"))
{
urlObj.Name = url.Remove(url.IndexOf('/'));
}
else
{
urlObj.Name = url.Remove(url.IndexOf('.'));
}
tabs.Insert(0, urlObj);
listBoxTabPage.ItemsSource = null;
listBoxTabPage.ItemsSource = tabs;
Browser.Navigate(new Uri("http://www.google.com", UriKind.Absolute));
//selectedTab = listBoxTabPage.SelectedValue as TabEntry;
}
private void ListBoxTabPage_SelectionChanged(object sender, GestureEventArgs e)
{
selectedTab = listBoxTabPage.SelectedValue as TabEntry;
Browser.Navigate(new Uri("http://www." + selectedTab.URL, UriKind.Absolute));
PivotItems.SelectedItem = BrowserPage;
}
Here is the code where it should update the selected tabs url, in the Browser_Navigated methord
void Browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
_deactivatedURL = e.Uri;
_progressIndicator.IsVisible = false;
string url = Convert.ToString(e.Uri);
//selectedTab.URL = url;
addHistoryRecord(url);
}
Where it is commented out I think is the problem. I believe the code does not know which one is the selectedTab. To fix this error should I create a methord which updates the url of the tab, each time the browser navigates. And how would the program know which tab is currently in use.
If you need any more details please comment and I will be happy to explain in further detail.
You just set listBoxTabPage.ItemsSource = null before you set tabs as ItemsSource. It may clears your listBoxTabPage.SelectedValue.
I think you can just set listBoxTabPage.SelectedValue = urlObj in addNewTab().
Then you begin navigate to a new uri in ListBoxTabPage_SelectionChanged, but it seems to be wrong that you just append a prefix string http://www. to an full url. It may triggers NavigateFailed event if you navigate to a non-exist website.
Browser.Navigate(new Uri("http://www." + selectedTab.URL, UriKind.Absolute));
What's more, ListBoxTabPage_SelectionChanged may triggers twice because the old value is cleared and the new value is set.

Change default tile image url

Is there any way I could change the Uris of the default tile images in the WMAppManifest.xml at runtime?
For example I would like to enable an option for users to select the tile image in the settings of my app. This is not the problem because I can then update the main tile with the new image if the app is pinned to the start, but if the app is not pinned and the user wants to pin the app another time then the default image will be used, and this is not the behavior I want, I want the tile to have the image the user selected in the settings. How to achieve this if possible?
I figured it out, I didn't know the ShellTile.ActiveTiles would always contain the default tile, whether the app is pinned or not, so I just updated this tile when the settings item changed:
private async void UpdateTile(bool isTransparent)
{
ShellTile defaultTile = ShellTile.ActiveTiles.FirstOrDefault();
if (defaultTile != null)
{
string tileFolder = isTransparent ? "Transparent" : "Normal";
defaultTile.Update(new FlipTileData()
{
SmallBackgroundImage = new Uri("appdata:/Assets/Tiles/" + tileFolder + "/Logo.scale-100.png"),
BackgroundImage = new Uri("appdata:/Assets/Tiles/" + tileFolder + "/Logo.scale-180.png"),
WideBackgroundImage = new Uri("appdata:/Assets/Tiles/" + tileFolder + "/WideLogo.scale-180.png")
});
}
}

How to go to the Windows Phone Start Screen directly from within my app?

My app has the option to create many secondary tiles. When user try to pin an existing tile, the app asks the user to go to the Start Screen to remove the tile first.
Is there a way programmatically to navigate to the WP Start Screen from my app?
UPDATE
I know user can just click the start button or back button. But I want to detect if that navigation is made to set certain flags. And it would be a little convenient for the user if the apps could help them to navigate automatically.
As you said "When user try to pin an existing tile" means you want to update the existing tile . Use the code for Updating the existing tile.
public static void UpdateAppTile(string navigationUri)
{
var tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(navigationUri));
if (tile != null)
{
var tileData = new FlipTileData
{
SmallBackgroundImage = new Uri("/Assets/Tiles/small.png", UriKind.Relative),
BackgroundImage = new Uri("/Assets/Tiles/medium.png", UriKind.Relative),
WideBackgroundImage = new Uri("/Assets/Tiles/wide.png", UriKind.Relative),
// other properties
};
tile.Update(tileData);
}
}
And for deleting the tile
public static void DeleteAppTile(string navigationUri)
{
var tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(navigationUri));
if (tile != null)
{
tileToFind.Delete();
}
}
From the UX point of view, you should already avoid such situations by disabling (or adding to the tile 'unpin from start' option) for the corresponding tile, if it's already pinned to start.
public static bool CheckIfTileExists(string tileUri)
{
ShellTile shellTile = ShellTile.ActiveTiles.FirstOrDefault(
tile => tile.NavigationUri.ToString().Contains(tileUri));
if (shellTile == null)
return false;
return true;
}

Monotouch - NavigationBar with extra padding ios 7

I'm trying to adapt my app for iOS 7. It's written with Xamarin and C#.
I'm having trouble with extra padding for the left button in the navigationbar.
I have a helper method for rendering my back-button which looks like this:
public static UIBarButtonItem GetBackButton (this UIViewController controller)
{
var backImage = new UIImage ("Images/back.png");
var backButton = new UIButton (UIButtonType.Custom);
backButton.Frame = new RectangleF (0, 0, 44, 44);
backButton.SetImage (backImage, UIControlState.Normal);
backButton.TouchUpInside += (object sender, EventArgs e) => {
var cancelBackNavigation = false;
if (controller is UIViewControllerBase) {
if (((UIViewControllerBase)controller).PrepareNavigateBack () != true) {
cancelBackNavigation = true;
}
}
if (cancelBackNavigation == false) {
controller.NavigationController.PopViewControllerAnimated (true);
}
};
return new UIBarButtonItem (backButton);
}
The navigationbar adds lots of padding before the back-button and making the image inside the back-button look very far away from its real position. The code above works fine in iOS 6.
I don't wanna use ContentEdgeInsets cause it will stretch the image and making it ugly.
Anyone with an idea of what to do?
I tried looking up your issue and found out that first, you need to hide the back button as follows:
NavigationItem.HidesBackButton = true;
Then for setting the button, you need to set it this way:
NavigationItem.BackBarButtonItem = yourButton;
That way, you won`t have the extra indentation.
Also you might find the following question useful:
Make a custom back button for UINavigationController
This is how I setup my NavigationBar
controller.View.BackgroundColor = Theme.BackgroundColor;
controller.NavigationItem.SetHidesBackButton (true, false);
controller.NavigationController.Toolbar.TintColor = Theme.BackgroundColor;
controller.NavigationController.NavigationBar.TintColor = Theme.BackgroundColor;
controller.NavigationController.SetNavigationBarHidden (show == false, false);
controller.NavigationController.NavigationBar.BackgroundColor = Theme.BackgroundColor;
controller.NavigationController.NavigationBar.SetTitleTextAttributes (Theme.NavigationBarTextAttributes);
controller.NavigationController.NavigationBar.Subviews [0].Alpha = 0.01f;

differentiating primary tile vs app-list wp8

I am developing an app for WP8.I want to know how to find out that my app is coming to foreground by clicking on the App-list or primary tile which user created.In a way I want to know how many users creates primary tile.
You can add a navigation parameter to your secondary tile like this :
string tileParameter = "Param=myParameter";
ShellTile tile = CheckIfTileExist(tileParameter);
if (tile == null)
{
StandardTileData secondaryTile = new StandardTileData
{
Title = tileParameter,
BackgroundImage = new Uri("Background-Secondary.png", UriKind.Relative),
Count = 2,
BackContent = "Secondary Tile Test"
};
ShellTile.Create(new Uri("/MainPage.xaml?" + tileParameter, UriKind.Relative), secondaryTile);
}
and then, in your MainPage's OnNavigatedTo event, you can get this parameter with something like this:
if (this.NavigationContext.QueryString.ContainsKey("Param"))
{
string param = this.NavigationContext.QueryString["Param"];//Get "Param" this query string.
//Do whatever you want with this parameter
}
Do you have a query string in your URI? If so you can check the NavigationContext.QueryString and see if the parameter used from your secondary tile is set or not.

Categories

Resources