Skip to content

Instantly share code, notes, and snippets.

View MartinZikmund's full-sized avatar
⌨️
Coding

Martin Zikmund MartinZikmund

⌨️
Coding
View GitHub Profile
@MartinZikmund
MartinZikmund / TextBoxTextChanging.xaml
Created January 22, 2019 22:35
Number-only TextBox with TextChanging filtering - XAML
<TextBox TextChanging="TextBox_OnTextChanging" />
@MartinZikmund
MartinZikmund / TextBoxTextChangingHandler.cs
Created January 22, 2019 22:37
Number-only TextBox Filtering
private void TextBox_OnTextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
sender.Text = new String(sender.Text.Where(char.IsDigit).ToArray());
}
@MartinZikmund
MartinZikmund / PicturesLibraryPermission.cs
Created January 31, 2019 13:00
Accessing pictures library in UWP (no error check)
var library = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
@MartinZikmund
MartinZikmund / TryAccessLibraryAsyncTryCatch.cs
Created January 31, 2019 13:06
Trying to access library with try...catch
private async Task<StorageLibrary> TryAccessLibraryAsync(KnownLibraryId library)
{
try
{
return await StorageLibrary.GetLibraryAsync(library);
}
catch (UnauthorizedAccessException)
{
...
}
@MartinZikmund
MartinZikmund / TryAccessLibraryAsyncMessageDialog.cs
Created January 31, 2019 13:07
Explain user what is happening
//explain the issue
MessageDialog requestPermissionDialog =
new MessageDialog($"The app needs to access the {library}. " +
"Press OK to open system settings and give this app permission. " +
"If the app closes, reopen it afterwards. " +
"If you Cancel, the app will have limited functionality only.");
//setup dialog commands
var okCommand = new UICommand("OK");
requestPermissionDialog.Commands.Add(okCommand);
@MartinZikmund
MartinZikmund / TryAccessLibraryAsyncCancel.cs
Created January 31, 2019 13:10
User canceled permission
if (requestPermissionResult == cancelCommand)
{
//user chose to Cancel, app will not have permission
return null;
}
//open app settings to allow users to give us permission
await Launcher.LaunchUriAsync(new Uri("ms-settings:appsfeatures-app"));
//confirmation dialog to retry
var confirmationDialog = new MessageDialog($"Please give this app the {library} permission " +
"in the Settings app which has now opened.");
confirmationDialog.Commands.Add(okCommand);
await confirmationDialog.ShowAsync();
@MartinZikmund
MartinZikmund / TryAccessLibraryAsync.cs
Last active December 25, 2020 15:52
Access library safely
private async Task<StorageLibrary> TryAccessLibraryAsync(KnownLibraryId library)
{
try
{
return await StorageLibrary.GetLibraryAsync(library);
}
catch (UnauthorizedAccessException)
{
//explain the issue
MessageDialog requestPermissionDialog =
@MartinZikmund
MartinZikmund / IsUserAGoat.cs
Last active February 3, 2019 14:54
Checks if user is a goat (subject to teleportations)
var userManager = (UserManager)GetSystemService(UserService);
if (userManager.IsUserAGoat)
{
//🐐
}
@MartinZikmund
MartinZikmund / IsUserAMonkey.cs
Last active February 3, 2019 14:54
Is user a monkey?
if (ActivityManager.IsUserAMonkey)
{
//🐒🐵🙊🙉🙈
}