Added ItemContainerInvokedBehavior
This commit is contained in:
@@ -31,7 +31,7 @@ public class SerialContext<TReader, TValue, TEvent>(IMessenger messenger,
|
||||
{
|
||||
await foreach (TValue value in reader.ReadAsync())
|
||||
{
|
||||
messenger.Send(new SerialEventArgs<TValue> { Value = value });
|
||||
messenger.Send(new TEvent { Value = value });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,7 @@ public class SerialStructReader(Stream stream) :
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
ReadResult? result = default;
|
||||
|
||||
ReadResult result;
|
||||
try
|
||||
{
|
||||
result = await reader.ReadAsync();
|
||||
@@ -31,21 +30,18 @@ public class SerialStructReader(Stream stream) :
|
||||
yield break;
|
||||
}
|
||||
|
||||
if (result.HasValue)
|
||||
ReadOnlySequence<byte> buffer = result.Buffer;
|
||||
while (TryParse(ref buffer, out SerialStructEventArgs serialEvent))
|
||||
{
|
||||
ReadOnlySequence<byte> buffer = result.Value.Buffer;
|
||||
while (TryParse(ref buffer, out SerialStructEventArgs serialEvent))
|
||||
{
|
||||
yield return serialEvent;
|
||||
}
|
||||
|
||||
reader.AdvanceTo(buffer.Start, buffer.End);
|
||||
yield return serialEvent;
|
||||
}
|
||||
|
||||
reader.AdvanceTo(buffer.Start, buffer.End);
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryParse(ref ReadOnlySequence<byte> buffer, out SerialStructEventArgs serialEvent)
|
||||
private bool TryParse(ref ReadOnlySequence<byte> buffer,
|
||||
out SerialStructEventArgs serialEvent)
|
||||
{
|
||||
SequenceReader<byte> reader = new(buffer);
|
||||
serialEvent = default!;
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using System;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using Microsoft.Xaml.Interactivity;
|
||||
|
||||
namespace Toolkit.UI.WinUI;
|
||||
|
||||
public class ItemContainerInvokedBehavior :
|
||||
Trigger<ItemContainer>
|
||||
{
|
||||
private ItemsView? itemsView;
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
base.OnAttached();
|
||||
if (AssociatedObject is not null)
|
||||
{
|
||||
AssociatedObject.Loaded += OnLoaded;
|
||||
AssociatedObject.Unloaded += OnUnloaded;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDetaching()
|
||||
{
|
||||
base.OnDetaching();
|
||||
if (AssociatedObject is not null)
|
||||
{
|
||||
AssociatedObject.Loaded -= OnLoaded;
|
||||
AssociatedObject.Unloaded -= OnUnloaded;
|
||||
}
|
||||
DetachFromItemsView();
|
||||
}
|
||||
|
||||
private void OnLoaded(object sender, RoutedEventArgs args)
|
||||
{
|
||||
if (AssociatedObject is not null)
|
||||
{
|
||||
itemsView = FindAncestor<ItemsView>(AssociatedObject);
|
||||
if (itemsView is not null)
|
||||
{
|
||||
itemsView.SelectionChanged += OnSelectionChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnUnloaded(object sender, RoutedEventArgs args)
|
||||
{
|
||||
DetachFromItemsView();
|
||||
if (AssociatedObject is not null)
|
||||
{
|
||||
AssociatedObject.Loaded -= OnLoaded;
|
||||
AssociatedObject.Unloaded -= OnUnloaded;
|
||||
}
|
||||
}
|
||||
|
||||
private void DetachFromItemsView()
|
||||
{
|
||||
if (itemsView is not null)
|
||||
{
|
||||
itemsView.SelectionChanged -= OnSelectionChanged;
|
||||
itemsView = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static T? FindAncestor<T>(DependencyObject current) where T : DependencyObject
|
||||
{
|
||||
while (current != null)
|
||||
{
|
||||
if (current is T ancestor)
|
||||
{
|
||||
return ancestor;
|
||||
}
|
||||
current = VisualTreeHelper.GetParent(current);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void OnSelectionChanged(ItemsView sender, ItemsViewSelectionChangedEventArgs args)
|
||||
{
|
||||
if (itemsView is not null && AssociatedObject is not null && AssociatedObject.DataContext == itemsView.SelectedItem)
|
||||
{
|
||||
Interaction.ExecuteActions(AssociatedObject, Actions, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using System;
|
||||
|
||||
namespace Toolkit.UI.WinUI;
|
||||
|
||||
public class ItemInvokedEventArgs : EventArgs;
|
||||
Reference in New Issue
Block a user