Skip to content

Commit

Permalink
fix: ArgumentOutOfRangeException when adding PrimaryCommand to NavBar (
Browse files Browse the repository at this point in the history
…#1161)

* fix: ArgumentOutOfRangeException when adding PrimaryCommand to NavigationBar

* chore: resolving PR comment

Co-authored-by: Xiaotian Gu <xiaoyao312@gmail.com>

---------

Co-authored-by: Xiaotian Gu <xiaoyao312@gmail.com>
  • Loading branch information
Kunal22shah and Xiaoy312 authored Jun 15, 2024
1 parent 3337789 commit cdc2afd
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions src/Uno.Toolkit.UI/Controls/NavigationBar/NavigationBarPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,32 +227,40 @@ private void OnNavBarSecondaryCommandsChanged(IObservableVector<ICommandBarEleme

private void OnCommandsChanged(IObservableVector<ICommandBarElement> sender, IVectorChangedEventArgs args, DependencyProperty prop)
{
if (_commandBar != null)
if (_commandBar == null) return;
var change = args.CollectionChange;
var changeIndex = args.Index;
if (_commandBar.GetValue(prop) is not IObservableVector<ICommandBarElement> commands) return;
if (change == CollectionChange.Reset)
{
commands.Clear();
}
else if (change == CollectionChange.ItemInserted)
{
var change = args.CollectionChange;
var changeIndex = args.Index;
var commands = _commandBar.GetValue(prop) as IObservableVector<ICommandBarElement>;
if (commands != null)
var element = sender[(int)changeIndex];
if (element != null)
{
if (change == CollectionChange.Reset)
{
commands.Clear();
}
else if (change == CollectionChange.ItemInserted ||
change == CollectionChange.ItemChanged)
{
var element = sender[(int)changeIndex];
if (element != null)
{
commands[(int)changeIndex] = element;
}
}
else if (change == CollectionChange.ItemRemoved)
commands.Insert((int)changeIndex, element);
}
}
else if (change == CollectionChange.ItemChanged)
{
if (changeIndex < commands.Count)
{
var element = sender[(int)changeIndex];
if (element != null)
{
commands.RemoveAt((int)changeIndex);
commands[(int)changeIndex] = element;
}
}
}
else if (change == CollectionChange.ItemRemoved)
{
if (changeIndex < commands.Count)
{
commands.RemoveAt((int)changeIndex);
}
}
}
}
}

0 comments on commit cdc2afd

Please sign in to comment.