Skip to content
Open
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
11 changes: 6 additions & 5 deletions WinUIGallery/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ private async void EnsureWindow()
var targetPageArguments = string.Empty;

AppActivationArguments eventArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
if (eventArgs != null && eventArgs.Kind == ExtendedActivationKind.Protocol && eventArgs.Data is ProtocolActivatedEventArgs)
if (eventArgs != null &&
eventArgs.Kind == ExtendedActivationKind.Protocol &&
eventArgs.Data is ProtocolActivatedEventArgs protocolArgs)
{
var ProtocolArgs = eventArgs.Data as ProtocolActivatedEventArgs;
string uri = ProtocolArgs.Uri.LocalPath.Replace("/", "");
string uri = protocolArgs.Uri.LocalPath.Replace("/", "");
targetPageArguments = uri;

if (uri == "AllControls")
Expand Down Expand Up @@ -155,15 +156,15 @@ private void HandleExceptions(object sender, Microsoft.UI.Xaml.UnhandledExceptio
if (NativeMethods.IsAppPackaged)
{
e.Handled = true; //Don't crash the app.

//Create the notification.
var notification = new AppNotificationBuilder()
.AddText("An exception was thrown.")
.AddText($"Type: {e.Exception.GetType()}")
.AddText($"Message: {e.Message}\r\n" +
$"HResult: {e.Exception.HResult}")
.BuildNotification();

//Show the notification
AppNotificationManager.Default.Show(notification);
}
Expand Down
52 changes: 25 additions & 27 deletions WinUIGallery/Controls/ControlExample.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ namespace WinUIGallery.Controls;
/// </summary>
public sealed class ControlExampleSubstitution : DependencyObject
{
public event TypedEventHandler<ControlExampleSubstitution, object> ValueChanged;
public event TypedEventHandler<ControlExampleSubstitution, object?>? ValueChanged;

public string Key { get; set; }
public string Key { get; set; } = string.Empty;

private object _value = null;
public object Value
private object? _value = null;
public object? Value
{
get { return _value; }
set
Expand Down Expand Up @@ -52,20 +52,15 @@ public string ValueAsString()
return string.Empty;
}

object value = Value;
object? value = Value;

// For solid color brushes, use the underlying color.
if (value is SolidColorBrush)
{
value = ((SolidColorBrush)value).Color;
}

if (value == null)
{
return string.Empty;
}

return value.ToString();
return value?.ToString() ?? string.Empty;
}
}

Expand Down Expand Up @@ -193,15 +188,19 @@ private enum SyntaxHighlightLanguage { Xml, CSharp };

private void SelectorBarItem_Loaded(object sender, RoutedEventArgs e)
{
var item = sender as SelectorBarItem;
if (item == null)
if (sender is not SelectorBarItem item)
return;

if (item.Tag.ToString().Equals("Xaml", StringComparison.OrdinalIgnoreCase))
PrepareSelectorBarItem(item);
}

private void PrepareSelectorBarItem(SelectorBarItem item)
{
if (item.Tag.ToString()?.Equals("Xaml", StringComparison.OrdinalIgnoreCase) is true)
{
item.Visibility = string.IsNullOrEmpty(Xaml) && string.IsNullOrEmpty(XamlSource) ? Visibility.Collapsed : Visibility.Visible;
}
else if (item.Tag.ToString().Equals("CSharp", StringComparison.OrdinalIgnoreCase))
else if (item.Tag.ToString()?.Equals("CSharp", StringComparison.OrdinalIgnoreCase) is true)
{
item.Visibility = string.IsNullOrEmpty(CSharp) && string.IsNullOrEmpty(CSharpSource) ? Visibility.Collapsed : Visibility.Visible;
}
Expand All @@ -217,19 +216,18 @@ private void SelectorBarItem_Loaded(object sender, RoutedEventArgs e)

private static void OnXamlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = (ControlExample)d;
if (ctrl != null)
{
ctrl.SelectorBarItem_Loaded(ctrl.SelectorBarXamlItem, null);
}
if (d is not ControlExample ctrl)
return;

ctrl.PrepareSelectorBarItem(ctrl.SelectorBarXamlItem);
}

private static void OnCSharpChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = (ControlExample)d;
if (ctrl != null)
{
ctrl.SelectorBarItem_Loaded(ctrl.SelectorBarCSharpItem, null);
}
if (d is not ControlExample ctrl)
return;

ctrl.PrepareSelectorBarItem(ctrl.SelectorBarCSharpItem);
}

private void SelectorBarControl_SelectionChanged(SelectorBar sender, SelectorBarSelectionChangedEventArgs args)
Expand All @@ -242,12 +240,12 @@ private void HandlePresenterVisibility()
var selectedItem = SelectorBarControl.SelectedItem;
if (selectedItem != null)
{
if (selectedItem.Tag.ToString().Equals("Xaml", StringComparison.OrdinalIgnoreCase))
if (selectedItem.Tag.ToString()?.Equals("Xaml", StringComparison.OrdinalIgnoreCase) is true)
{
XamlContentPresenter.Visibility = Visibility.Visible;
CSharpContentPresenter.Visibility = Visibility.Collapsed;
}
else if (selectedItem.Tag.ToString().Equals("CSharp", StringComparison.OrdinalIgnoreCase))
else if (selectedItem.Tag.ToString()?.Equals("CSharp", StringComparison.OrdinalIgnoreCase) is true)
{
CSharpContentPresenter.Visibility = Visibility.Visible;
XamlContentPresenter.Visibility = Visibility.Collapsed;
Expand Down
5 changes: 4 additions & 1 deletion WinUIGallery/Controls/DesignGuidance/ColorTile.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ private void CopyBrushNameButton_Click(object sender, RoutedEventArgs e)
package.SetText(ColorBrushName);
Clipboard.SetContent(package);

UIHelper.AnnounceActionForAccessibility(sender as Button, "Brush name copied to clipboard", "BrushNameCopiedSuccessNotificationId");
if (sender is Button button)
{
UIHelper.AnnounceActionForAccessibility(button, "Brush name copied to clipboard", "BrushNameCopiedSuccessNotificationId");
}
}
}
2 changes: 1 addition & 1 deletion WinUIGallery/Controls/InlineColorPicker.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public SolidColorBrush ColorBrush
public static readonly DependencyProperty ColorBrushProperty =
DependencyProperty.Register("ColorBrush", typeof(SolidColorBrush), typeof(InlineColorPicker), new PropertyMetadata(new SolidColorBrush(Microsoft.UI.Colors.White)));

public event EventHandler<Color> ColorChanged;
public event EventHandler<Color>? ColorChanged;

public InlineColorPicker()
{
Expand Down
19 changes: 12 additions & 7 deletions WinUIGallery/Controls/PageHeader.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public Visibility ThemeButtonVisibility
public static readonly DependencyProperty ThemeButtonVisibilityProperty =
DependencyProperty.Register("ThemeButtonVisibility", typeof(Visibility), typeof(PageHeader), new PropertyMetadata(Visibility.Visible));

public string PageName { get; set; }
public Action CopyLinkAction { get; set; }
public Action ToggleThemeAction { get; set; }
public string PageName { get; set; } = string.Empty;
public Action? CopyLinkAction { get; set; }
public Action? ToggleThemeAction { get; set; }

public ControlInfoDataItem Item
public ControlInfoDataItem? Item
{
get { return _item; }
set { _item = value; }
}

private ControlInfoDataItem _item;
private ControlInfoDataItem? _item;

public PageHeader()
{
Expand Down Expand Up @@ -86,6 +86,11 @@ private void OnCopyDontShowAgainButtonClick(TeachingTip sender, object args)

private void OnCopyLink()
{
if (this.Item is null)
{
return;
}

ProtocolActivationClipboardHelper.Copy(this.Item);
}
public async void OnFeedBackButtonClick(object sender, RoutedEventArgs e)
Expand All @@ -107,12 +112,12 @@ private void UserControl_Loaded(object sender, RoutedEventArgs e)

private string GetFavoriteGlyph(bool? isFavorite)
{
return (bool)isFavorite ? "\uE735" : "\uE734";
return isFavorite is true ? "\uE735" : "\uE734";
}

private string GetFavoriteToolTip(bool? isFavorite)
{
return (bool)isFavorite ? "Remove from favorites" : "Add to favorites";
return isFavorite is true ? "Remove from favorites" : "Add to favorites";
}

private void FavoriteButton_Click(object sender, RoutedEventArgs e)
Expand Down
13 changes: 6 additions & 7 deletions WinUIGallery/Controls/SampleCodePresenter.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public IList<ControlExampleSubstitution> Substitutions

private string actualCode = "";
private static Regex SubstitutionPattern = new Regex(@"\$\(([^\)]+)\)");
private RichTextBlock sampleCodeRTB;
private RichTextBlock? sampleCodeRTB;

public SampleCodePresenter()
{
Expand Down Expand Up @@ -126,7 +126,7 @@ private void SampleCodePresenter_ActualThemeChanged(FrameworkElement sender, obj
GenerateSyntaxHighlightedContent();
}

private void OnValueChanged(ControlExampleSubstitution sender, object e)
private void OnValueChanged(ControlExampleSubstitution sender, object? _)
{
GenerateSyntaxHighlightedContent();
}
Expand Down Expand Up @@ -166,8 +166,7 @@ private async void FormatAndRenderSampleFromFile(string sourceRelativePath, Cont
{
if (sourceRelativePath != null && sourceRelativePath.EndsWith("txt"))
{
string sampleString = null;
StorageFile file = null;
StorageFile? file = null;
if (!NativeMethods.IsAppPackaged)
{
var relativePath = GetDerivedSourceUnpackaged(sourceRelativePath);
Expand All @@ -182,7 +181,7 @@ private async void FormatAndRenderSampleFromFile(string sourceRelativePath, Cont
file = await StorageFile.GetFileFromApplicationUriAsync(derivedSource);
}

sampleString = await FileIO.ReadTextAsync(file);
string sampleString = await FileIO.ReadTextAsync(file);

FormatAndRenderSampleFromString(sampleString, presenter, highlightLanguage);
}
Expand Down Expand Up @@ -319,7 +318,7 @@ private void CopyCodeButton_Click(object sender, RoutedEventArgs e)

private void CodeScrollViewer_Loaded(object sender, RoutedEventArgs e)
{
ScrollBar horizontalScrollBar = FindHorizontalScrollBar(CodeScrollViewer);
ScrollBar? horizontalScrollBar = FindHorizontalScrollBar(CodeScrollViewer);
if (horizontalScrollBar != null)
{
// Create a timer and store it in the ScrollBar's Tag property.
Expand Down Expand Up @@ -361,7 +360,7 @@ private void HorizontalScrollBar_Scroll(object sender, ScrollEventArgs e)
}
}

private ScrollBar FindHorizontalScrollBar(DependencyObject element)
private ScrollBar? FindHorizontalScrollBar(DependencyObject element)
{
if (element is ScrollBar sb && sb.Orientation == Orientation.Horizontal)
{
Expand Down
6 changes: 3 additions & 3 deletions WinUIGallery/Converters/MenuItemTemplateSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace WinUIGallery.Converters;
[ContentProperty(Name = "ItemTemplate")]
partial class MenuItemTemplateSelector : DataTemplateSelector
{
public DataTemplate ItemTemplate { get; set; }
public DataTemplate? ItemTemplate { get; set; }

protected override DataTemplate SelectTemplateCore(object item)
protected override DataTemplate? SelectTemplateCore(object item)
{
return item is Separator ? SeparatorTemplate : item is Header ? HeaderTemplate : ItemTemplate;
}

protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
protected override DataTemplate? SelectTemplateCore(object item, DependencyObject container)
{
return item is Separator ? SeparatorTemplate : item is Header ? HeaderTemplate : ItemTemplate;
}
Expand Down
17 changes: 10 additions & 7 deletions WinUIGallery/Helpers/ControlInfoDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<IEnumerable<ControlInfoDataGroup>> GetGroupsAsync()
return _instance.Groups;
}

public static async Task<ControlInfoDataGroup> GetGroupAsync(string uniqueId)
public static async Task<ControlInfoDataGroup?> GetGroupAsync(string uniqueId)
{
await _instance.GetControlInfoDataAsync();
// Simple linear search is acceptable for small data sets
Expand All @@ -63,7 +63,7 @@ public static async Task<ControlInfoDataGroup> GetGroupAsync(string uniqueId)
return null;
}

public static async Task<ControlInfoDataItem> GetItemAsync(string uniqueId)
public static async Task<ControlInfoDataItem?> GetItemAsync(string uniqueId)
{
await _instance.GetControlInfoDataAsync();
// Simple linear search is acceptable for small data sets
Expand All @@ -72,7 +72,7 @@ public static async Task<ControlInfoDataItem> GetItemAsync(string uniqueId)
return null;
}

public static async Task<ControlInfoDataGroup> GetGroupFromItemAsync(string uniqueId)
public static async Task<ControlInfoDataGroup?> GetGroupFromItemAsync(string uniqueId)
{
await _instance.GetControlInfoDataAsync();
var matches = _instance.Groups.Where((group) => group.Items.FirstOrDefault(item => item.UniqueId.Equals(uniqueId)) != null);
Expand All @@ -93,27 +93,30 @@ private async Task GetControlInfoDataAsync()
var jsonText = await FileLoader.LoadText("Samples/Data/ControlInfoData.json");
var controlInfoDataGroup = JsonSerializer.Deserialize(jsonText, typeof(Root), RootContext.Default) as Root;

if (controlInfoDataGroup is null)
{
return;
}

lock (_lock)
{
string pageRoot = "WinUIGallery.ControlPages.";

controlInfoDataGroup.Groups.SelectMany(g => g.Items).ToList().ForEach(item =>
{
#nullable enable
string? badgeString = item switch
string badgeString = item switch
{
{ IsNew: true } => "New",
{ IsUpdated: true } => "Updated",
{ IsPreview: true } => "Preview",
_ => null
_ => string.Empty,
};
string pageString = $"{pageRoot}{item.UniqueId}Page";
Type? pageType = Type.GetType(pageString);

item.BadgeString = badgeString;
item.IncludedInBuild = pageType is not null;
item.ImagePath ??= "ms-appx:///Assets/ControlImages/Placeholder.png";
#nullable disable
});

foreach (var group in controlInfoDataGroup.Groups)
Expand Down
2 changes: 1 addition & 1 deletion WinUIGallery/Helpers/FileLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal partial class FileLoader
{
public static async Task<string> LoadText(string relativeFilePath)
{
StorageFile file = null;
StorageFile? file = null;
if (!NativeMethods.IsAppPackaged)
{
var sourcePath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, relativeFilePath));
Expand Down
6 changes: 4 additions & 2 deletions WinUIGallery/Helpers/IconsDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ public async Task<List<IconData>> LoadIcons()
var jsonText = await FileLoader.LoadText("Samples/Data/IconsData.json");
lock (_lock)
{
if (icons.Count == 0)
if (icons.Count == 0 &&
JsonSerializer.Deserialize(jsonText, typeof(List<IconData>), IconDataListContext.Default) is List<IconData> loadedIcons)
{
icons = JsonSerializer.Deserialize(jsonText, typeof(List<IconData>), IconDataListContext.Default) as List<IconData>;
icons = loadedIcons;
}

return icons;
}
}
Expand Down
Loading