locked
using event handler for DeviceWatcher

    Question

  • Hello

    I cant get how to make work DeviceWatcher->Added

    I try to make class similar to this:

    namespace WUXD = Windows::UI::Xaml::Data;
      [WUXD::Bindable]
      public ref class Person sealed : WUXD::INotifyPropertyChanged
      {
      public:
        Person(void);
        property Platform::String^ Name {
          Platform::String^ get();
          void set(Platform::String^ value);
        }
        property Platform::String^ Phone;
        virtual event WUXD::PropertyChangedEventHandler^ PropertyChanged;
      private:
        void NotifyPropertyChanged(Platform::String^ prop);
        Platform::String^ m_Name;
      };

    but, DeviceWatcher has no anything like "INotifyPropertyChanged" and "PropertyChangedEventHandler".

    So i tried to get how EventHandler works, but i cant. Can anybody help me?

    Also, previously to this, i enumerating devices with Windows::Devices::Enumeration::DeviceInformatio::FindAllAsync(param)

    Without param it returnes unplugged devices, but any param i try returns only plugged  devices (enumerting audio endpoint devices). Is there any way to get all audio endpoint devices (plugged + unplugged)?

    Tuesday, March 25, 2014 10:34 AM

All replies

  • to let added work this code would be enough?

    MainPage::MainPage()
    {
    	InitializeComponent();
    
    	DeviceWatcher^ watcher = DeviceInformation::CreateWatcher();
    	watcher->Added += ref new Windows::Foundation::TypedEventHandler<Windows::Devices::Enumeration::DeviceWatcher ^, Windows::Devices::Enumeration::DeviceInformation ^>(this, &App2::MainPage::OnAdded);
    }
    
    
    void App2::MainPage::OnAdded(Windows::Devices::Enumeration::DeviceWatcher ^sender, Windows::Devices::Enumeration::DeviceInformation ^args)
    {
    	
    }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, March 25, 2014 3:36 PM
  • as i now, after Start it raises "Added" on each new device.

    i add to 

    MainPage::MainPage() { InitializeComponent(); DeviceWatcher^ watcher = DeviceInformation::CreateWatcher(); watcher->Added += ref new Windows::Foundation::TypedEventHandler<Windows::Devices::Enumeration::DeviceWatcher ^, Windows::Devices::Enumeration::DeviceInformation ^>(this, &App2::MainPage::OnAdded);

    //my add

    watcher->Start(); } void App2::MainPage::OnAdded(Windows::Devices::Enumeration::DeviceWatcher ^sender, Windows::Devices::Enumeration::DeviceInformation ^args) {

    //my add listview->Items->Append(args->Name->ToString()); }

    but added isnt raised. So what i doing wrong?

    Also, where i should init var so it might be seen under OnAdded func?
    • Edited by Romka2411 Tuesday, March 25, 2014 4:34 PM
    Tuesday, March 25, 2014 4:32 PM
  • i think you have to run the list-items-append in the dispatcher

    this

    ->Dispatcher->RunAsync()


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, March 25, 2014 4:49 PM
  • do you mean i need make it like this:

    listview->Dispatcher->RunAsync([this]{listview->Items->Append()}); ?

    i dont think problem here, i indicate when OnAdded runs and change text in textblock and check listview->Items-Size. there still nothing

    maybe i got something wrong. Added runs only when new device appear in system, or it also runs when device change state of plugged\unplugged?

    i just want to keep list of all (plugged + unplugged) audio devices and have a callback when default is changed (i saw it in Windows::Media::Device::MediaDevice) and also an event, when appear non-default adio device

    Wednesday, March 26, 2014 5:12 AM