Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions SampleUserControlLibrary/SampleUserControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public string SubscriptionKey
set;
}

public string EndpointAddress
{
get;
set;
}

public SampleScenarios()
{
InitializeComponent();
Expand Down
33 changes: 24 additions & 9 deletions SampleUserControlLibrary/SubscriptionKeyPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,32 @@
</Style>
</Page.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock Margin="5, 0, 5, 0" >To use the service, you need to ensure that you have right subscription key.</TextBlock>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Grid.ColumnSpan="4" Orientation="Vertical" >
<TextBlock Grid.Row="0" Margin="5, 0, 5, 0" >To use the service, you need to ensure that you have right subscription key.</TextBlock>
<TextBlock Margin="5, 0, 5, 0" >Please note that each service (Face, Emotion, Speech, etc) has its own subscription key.</TextBlock>
<TextBlock Margin="5, 0, 5, 0" >You must specify the endpoint that matches the API key.</TextBlock>
<TextBlock Margin="5, 0, 5, 0" >If you do not have key yet, please use the link to get a key first, then paste the key into the textbox below.</TextBlock>
<Button Grid.Row="1" Grid.Column="3" Click="GetKeyButton_Click" HorizontalAlignment="Left" Style="{StaticResource LinkButton}" Margin="5, 0, 0, 0" Content="Get Key" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
<Label VerticalAlignment="Center">Subscription Key:</Label>
<TextBox VerticalAlignment="Stretch" Width="300" Padding="2" Text="{Binding Path=SubscriptionKey, Mode=TwoWay}" Margin="0,5,0,5"/>
<Button Click="SaveKey_Click" Margin="5, 0, 0, 0" Padding="5, 0, 5, 0" VerticalAlignment="Center" >Save Key</Button>
<Button Click="DeleteKey_Click" Margin="5, 0, 0, 0" Padding="5, 0, 5, 0" VerticalAlignment="Center" >Delete Key</Button>
</StackPanel>
<Button Click="GetKeyButton_Click" HorizontalAlignment="Left" Style="{StaticResource LinkButton}" Margin="5, 0, 0, 0" Content="Get Key" />
</StackPanel>

<Label Grid.Row="1" Grid.Column="0" VerticalAlignment="Center">Subscription Key:</Label>
<TextBox Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch" Padding="2" Text="{Binding Path=SubscriptionKey, Mode=TwoWay}" Margin="0,5,0,5"/>

<Label Grid.Row="2" Grid.Column="0" VerticalAlignment="Center">Endpoint Address:</Label>
<TextBox Grid.Row="2" Grid.Column="1" VerticalAlignment="Stretch" Padding="2" Text="{Binding Path=EndpointAddress, Mode=TwoWay}" Margin="0,5,0,5"/>

<Button Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" Click="Save_Click" Margin="4, 0, 4, 0" Padding="5, 0, 5, 0" VerticalAlignment="Center" >Save</Button>
<Button Grid.Row="1" Grid.Column="3" Grid.RowSpan="2" Click="Delete_Click" Margin="4, 0, 4, 0" Padding="5, 0, 5, 0" VerticalAlignment="Center" >Delete</Button>
</Grid>
</Page>
82 changes: 56 additions & 26 deletions SampleUserControlLibrary/SubscriptionKeyPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.ComponentModel;
using System.IO;
using System.IO.IsolatedStorage;
Expand All @@ -47,8 +48,10 @@ public partial class SubscriptionKeyPage : Page, INotifyPropertyChanged
{
private readonly string _isolatedStorageSubscriptionKeyFileName = "Subscription.txt";
private readonly string _defaultSubscriptionKeyPromptMessage = "Paste your subscription key here to start";
private readonly string _defaultEndpointAddressPromptMessage = "Paste your endpoint address here to start";

private static string s_subscriptionKey;
private static string s_endpointAddress;

private SampleScenarios _sampleScenarios;
public SubscriptionKeyPage(SampleScenarios sampleScenarios)
Expand All @@ -57,7 +60,9 @@ public SubscriptionKeyPage(SampleScenarios sampleScenarios)
_sampleScenarios = sampleScenarios;

DataContext = this;
SubscriptionKey = GetSubscriptionKeyFromIsolatedStorage();
var info = GetSubscriptionInfoFromIsolatedStorage();
SubscriptionKey = info.Item1;
EndpointAddress = info.Item2;
}

/// <summary>
Expand All @@ -78,6 +83,24 @@ public string SubscriptionKey
}
}

/// <summary>
/// Gets or sets subscription key
/// </summary>
public string EndpointAddress
{
get
{
return s_endpointAddress;
}

set
{
s_endpointAddress = value;
OnPropertyChanged<string>();
_sampleScenarios.EndpointAddress = s_endpointAddress;
}
}

/// <summary>
/// Implement INotifyPropertyChanged interface
/// </summary>
Expand All @@ -101,10 +124,11 @@ private void OnPropertyChanged<T>([CallerMemberName]string caller = null)
/// <summary>
/// Gets the subscription key from isolated storage.
/// </summary>
/// <returns></returns>
private string GetSubscriptionKeyFromIsolatedStorage()
/// <returns>Tuple of (subscription-key, endpoint-address)</returns>
private Tuple<string, string> GetSubscriptionInfoFromIsolatedStorage()
{
string subscriptionKey = null;
string endpointAddress = null;

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
{
Expand All @@ -115,26 +139,35 @@ private string GetSubscriptionKeyFromIsolatedStorage()
using (var reader = new StreamReader(iStream))
{
subscriptionKey = reader.ReadLine();
if (!reader.EndOfStream)
{
endpointAddress = reader.ReadLine();
}
}
}
}
catch (FileNotFoundException)
{
subscriptionKey = null;
// Override below
}
}
if (string.IsNullOrEmpty(subscriptionKey))
{
subscriptionKey = _defaultSubscriptionKeyPromptMessage;
}
return subscriptionKey;
if (string.IsNullOrEmpty(endpointAddress))
{
endpointAddress = _defaultEndpointAddressPromptMessage;
}
return new Tuple<string, string>(subscriptionKey, endpointAddress);
}

/// <summary>
/// Saves the subscription key to isolated storage.
/// </summary>
/// <param name="subscriptionKey">The subscription key.</param>
private void SaveSubscriptionKeyToIsolatedStorage(string subscriptionKey)
/// <param name="endpointAddress">Endpoint address</param>
private void SaveSubscriptionKeyToIsolatedStorage(string subscriptionKey, string endpointAddress)
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
{
Expand All @@ -143,48 +176,45 @@ private void SaveSubscriptionKeyToIsolatedStorage(string subscriptionKey)
using (var writer = new StreamWriter(oStream))
{
writer.WriteLine(subscriptionKey);
writer.WriteLine(endpointAddress);
}
}
}
}

/// <summary>
/// Handles the Click event of the subscription key save button.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
private void SaveKey_Click(object sender, RoutedEventArgs e)
private void GetKeyButton_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("https://www.microsoft.com/cognitive-services/en-us/sign-up");
}

private void Save_Click(object sender, RoutedEventArgs e)
{
try
{
SaveSubscriptionKeyToIsolatedStorage(SubscriptionKey);
MessageBox.Show("Subscription key is saved in your disk.\nYou do not need to paste the key next time.", "Subscription Key");
SaveSubscriptionKeyToIsolatedStorage(SubscriptionKey, EndpointAddress);
MessageBox.Show("Subscription info is saved in your disk.\nYou do not need to paste the key next time.", "Subscription Info");
}
catch (System.Exception exception)
{
MessageBox.Show("Fail to save subscription key. Error message: " + exception.Message,
"Subscription Key", MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show("Fail to save subscription info. Error message: " + exception.Message,
"Subscription Info", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

private void DeleteKey_Click(object sender, RoutedEventArgs e)
private void Delete_Click(object sender, RoutedEventArgs e)
{
try
{
SubscriptionKey = _defaultSubscriptionKeyPromptMessage;
SaveSubscriptionKeyToIsolatedStorage("");
MessageBox.Show("Subscription key is deleted from your disk.", "Subscription Key");
EndpointAddress = _defaultEndpointAddressPromptMessage;
SaveSubscriptionKeyToIsolatedStorage("", "");
MessageBox.Show("Subscription info is deleted from your disk.", "Subscription Info");
}
catch (System.Exception exception)
{
MessageBox.Show("Fail to delete subscription key. Error message: " + exception.Message,
"Subscription Key", MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show("Fail to delete subscription info. Error message: " + exception.Message,
"Subscription Info", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

private void GetKeyButton_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("https://www.microsoft.com/cognitive-services/en-us/sign-up");
}
}
}