Finally i found the solution. on google :)
class DeviceWatcherHelper
{
public DeviceWatcherHelper(
ObservableCollection<DeviceInformationDisplay> resultCollection,
CoreDispatcher dispatcher)
{
this.resultCollection = resultCollection;
this.dispatcher = dispatcher;
}
public delegate void DeviceChangedHandler(DeviceWatcher deviceWatcher, string id);
public event DeviceChangedHandler DeviceChanged;
public DeviceWatcher DeviceWatcher => deviceWatcher;
public bool UpdateStatus = true;
public void StartWatcher(DeviceWatcher deviceWatcher)
{
this.deviceWatcher = deviceWatcher;
deviceWatcher.Added += Watcher_DeviceAdded;
deviceWatcher.Updated += Watcher_DeviceUpdated;
deviceWatcher.Removed += Watcher_DeviceRemoved;
deviceWatcher.Start();
}
public void StopWatcher()
{
if (IsWatcherStarted(deviceWatcher))
{
deviceWatcher.Stop();
}
}
public void Reset()
{
if (deviceWatcher != null)
{
StopWatcher();
deviceWatcher = null;
}
}
DeviceWatcher deviceWatcher = null;
ObservableCollection<DeviceInformationDisplay> resultCollection;
CoreDispatcher dispatcher;
static bool IsWatcherStarted(DeviceWatcher watcher)
{
return (watcher.Status == DeviceWatcherStatus.Started) ||
(watcher.Status == DeviceWatcherStatus.EnumerationCompleted);
}
public bool IsWatcherRunning()
{
if (deviceWatcher == null)
{
return false;
}
DeviceWatcherStatus status = deviceWatcher.Status;
return (status == DeviceWatcherStatus.Started) ||
(status == DeviceWatcherStatus.EnumerationCompleted) ||
(status == DeviceWatcherStatus.Stopping);
}
private async void Watcher_DeviceAdded(DeviceWatcher sender, DeviceInformation deviceInfo)
{
await dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
{
if (IsWatcherStarted(sender))
{
resultCollection.Add(new DeviceInformationDisplay(deviceInfo ));
}
});
}
private async void Watcher_DeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
{
await dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
{
if (IsWatcherStarted(sender))
{
foreach (DeviceInformationDisplay deviceInfoDisp in resultCollection)
{
if (deviceInfoDisp.Id == deviceInfoUpdate.Id)
{
deviceInfoDisp.Update(deviceInfoUpdate);
break;
}
}
}
});
}
private async void Watcher_DeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
{
await dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
{
if (IsWatcherStarted(sender))
{
foreach (DeviceInformationDisplay deviceInfoDisp in resultCollection)
{
if (deviceInfoDisp.Id == deviceInfoUpdate.Id)
{
resultCollection.Remove(deviceInfoDisp);
break;
}
}
}
});
}
private async void Watcher_EnumerationCompleted(DeviceWatcher sender, object obj)
{
await dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
{
});
}
private async void Watcher_Stopped(DeviceWatcher sender, object obj)
{
await dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
{
});
}
}
on Scan Document Page XAML code :
<Border extensions:Mouse.Cursor="Hand" Margin="42,10,76,0" BorderThickness="1" BorderBrush="#E1E1E1" CornerRadius="5" VerticalAlignment="Stretch">
<ComboBox Padding="5 0 0 0" Name="CmbScannerList" extensions:Mouse.Cursor="Hand" SelectedValuePath="rid" BorderBrush="Transparent" BorderThickness="0" FontSize="13" FontWeight="Light" FontFamily="{StaticResource inventoryRegularFont}" TabIndex="8" Header="" PlaceholderForeground="#414042" PlaceholderText="Connected Devices List " HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,-1,0,0" Width="532" Height="57">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="local:DeviceInformationDisplay" >
<TextBlock Name="{x:Bind Id }" Text="{x:Bind Name }" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Border>
scan document page code behind:
public sealed partial class ScanDocumentPage : Page
{
private DeviceWatcherHelper deviceWatcherHelper;
private ObservableCollection<DeviceInformationDisplay> resultCollection = new ObservableCollection<DeviceInformationDisplay>();
public ScanDocumentPage()
{
this.InitializeComponent();
deviceWatcherHelper = new DeviceWatcherHelper(resultCollection, Dispatcher);
StartWatcher();
}
private void StartWatcher()
{
resultCollection.Clear();
DeviceWatcher deviceWatcher;
deviceWatcher = DeviceInformation.CreateWatcher(DeviceClass.All );
deviceWatcherHelper.StartWatcher(deviceWatcher);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
CmbScannerList.ItemsSource = resultCollection;
}