Custom App Preferences in Windows Phone 8 C#

I wanted to be able to determine the volume state of the Windows Phone 8 device, and searching around on the internet proved useless, not to mention the fact there’s a couple of settings in the SDK that look like they might be what I’m looking for, but documentation and blogs on Windows 7.1 say it’s not possible, so I’m not getting my hopes up. If anyone knows of a way to determine if a Windows Phone 8 device is set to vibrate or silent vs. not (in C#) please post a comment!

In the meantime I added a button to the app using IsolatedStorageSettings. This can be used for virtually anything you want to store locally without having to use a database or other config or settings file. I implemented this like so:

try
{
    muted = Convert.ToBoolean(IsolatedStorageSettings.ApplicationSettings["Key"]);
}
catch
{
    IsolatedStorageSettings.ApplicationSettings["Key"] = 0;
    IsolatedStorageSettings.ApplicationSettings.Save();
}

Just substitute your key name where you see the word “Key” above. This gave me the ability to toggle the app’s sound settings on click by doing something like this on the “Tap” event:

private void ChangeSound(object sender, System.Windows.Input.GestureEventArgs e)
{
    if (IsolatedStorageSettings.ApplicationSettings[Key].ToString() == "0")
    {
        IsolatedStorageSettings.ApplicationSettings["Key"] = 1;
        IsolatedStorageSettings.ApplicationSettings.Save();
        Sound.Source = new BitmapImage(new Uri("Assets/soundoff.png", UriKind.Relative)); 
        muted = Convert.ToBoolean(IsolatedStorageSettings.ApplicationSettings["Key"]);
    }
    else
    {
        IsolatedStorageSettings.ApplicationSettings["Key"] = 0;
        IsolatedStorageSettings.ApplicationSettings.Save();
        Sound.Source = new BitmapImage(new Uri("Assets/soundon.png", UriKind.Relative));
        muted = Convert.ToBoolean(IsolatedStorageSettings.ApplicationSettings["Key"]);
    }
}

Now, you may be wondering if there’s a class to handle many settings? Well, I’m not aware of anything built into the SDK, but I did find something that could help. A StackOverflow post revealed a simple class that handles this fairly well:

public static class AppSettings
{
    private static IsolatedStorageSettings Settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;

    public static void StoreSetting(string settingName, string value)
    {
        StoreSetting(settingName, value);
    }

    public static void StoreSetting(string settingName, TValue value)
    {
        if (!Settings.Contains(settingName))
            Settings.Add(settingName, value);
        else
            Settings[settingName] = value;
        Settings.Save();
    }

    public static bool TryGetSetting(string settingName, out TValue value)
    {            
        if (Settings.Contains(settingName))
        {
            value = (TValue)Settings[settingName];
            return true;
        }

        value = default(TValue);
        return false;
    }
}

Thanks to Jacob for that one! Hope this helps

Eric

Eric Oszakiewski is a professional software developer based in Scottsdale, AZ with over 35 years of IT experience, and 19 years Native American Gaming experience. He is currently working as a Sr .Net/SharePoint Developer for General Motors, and also as a consultant.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

Stop and restart specific item workflows in SharePoint 2010 Using Powershell

While sending our company in a paperless direction, I found that as I published updated workflows to a library old versions were not only still running (as expected) but becoming corrupt, almost like they were getting “stale”. These stale workflows would behave abnormally the more I published changes to the same workflow. Since these were approval-style workflows, where various selected staff were supposed to complete task items, things weren’t getting completed, and the workflows were seeming to be sitting there, doing nothing.

The example here is applying to employee appraisals. With over 3000 employees, finding these “stuck” workflows on items, terminating them, then restarting them is not something anyone wants to do. So I came up with this Powershell script I run on the server that stops all old running instances of any workflows on the affected item, then prompts to restart the most recent version of the same workflow.

cls
$fc = $host.UI.RawUI.ForegroundColor
$web = "";
$host.UI.RawUI.ForegroundColor = "Yellow"
$site = Get-SPSite "";
$manager = $site.WorkFlowManager;
$targetweb = Read-Host "Enter the full URL of the site where the item is hosted";
$targetlist = Read-Host "Enter the name of the List where you want to search for the item";	
$itemname = Read-Host "Item name you are looking for? (partial name ok)";
write-Output "";

try 
{
	$web = Get-SPWeb $targetweb; 
	if($?)
	{}
	else
	{
	$host.UI.RawUI.ForegroundColor = "Red"
	write-Output "Cannot connect to that site, please try again";
	$host.UI.RawUI.ForegroundColor = $fc
	}
}
catch{}
$cnt = 0;
$web.AllowUnsafeUpdates = $true;      

#List Name  
$list = $web.Lists[$targetlist];  
$cnt = 0;
	
$host.UI.RawUI.ForegroundColor = $fc

# Iterate through all Items in List and all Workflows on Items.           
foreach ($item in $list.Items) 
{ 
	if($item["Title"] -like "*"+$itemname+"*")
	{ 
		foreach ($wf in $item.Workflows) 
		{  
			if(($wf.InternalState -ne "Completed") -and $wf.InternalState -ne "Cancelled"))
			{
				#Show status
				$x = $wf.ItemName.TrimEnd();
				write-Output $x;
				$cnt = $cnt + 1;
			}
			if($cnt -ne 0) 
			{
				$results = "This item has " + $cnt.tostring() + " workflows running.";
				write-Output "";
				write-Output $results;
				$host.UI.RawUI.ForegroundColor = "Red"
				$input = Read-Host "Do you want to stop all running workflows for this item and restart a new one? (Y/N)";
				$host.UI.RawUI.ForegroundColor = $fc
				if($input -ne "Y" -and $input -ne "y" -and $input -ne "YES" -and $input -ne "Yes" -and $input -ne "yes")
				{
					$cnt = 0;
				}
				else 
				{
					$cnt = 0;
					foreach ($wf in $item.Workflows) 
					{  
						if($wf.InternalState -ne "Completed" -and $wf.InternalState -ne "Cancelled")
						{
							# Stop workflow
						[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);  
							$cnt = $cnt + 1;
						}
					}
					# Restart the workflow
					#
					$association = $list.WorkFlowAssociations | where {$_.Name -eq ""}
					$association.AllowAsyncManualStart = $true
					$association.AllowManual = $true
					$data = $association.AssociationData;
					$newwf = $manager.StartWorkflow($item, $association, $data);
					$host.UI.RawUI.ForegroundColor = "Yellow"
					$results = $cnt.tostring() + " workflow(s) stopped & 1 new workflow restarted";
					write-Output "";
					write-Output $results;
					$host.UI.RawUI.ForegroundColor = $fc
					$cnt = 0;
				}
			} 
		}
	}
}
$web.Dispose();
$site.Dispose();
write-Output "";

The result was a nice, clean console UI that prompted for every item similar to the name I provided in the beginning, doing a fuzzy search and allowing me to either respond yes or no for each item. Any outstanding workflows for that selected item are terminated, and a new one is started. Note this is designed to work on lists/libraries with one workflow, such as an approval workflow. You may need to modify this code slightly for lists/libraries with multiple workflows, to make sure you terminate the correct one. Enjoy!

Eric

Eric Oszakiewski is a professional software developer based in Scottsdale, AZ with over 35 years of IT experience, and 19 years Native American Gaming experience. He is currently working as a Sr .Net/SharePoint Developer for General Motors, and also as a consultant.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube