LampArray.AvailabilityChanged 이벤트

정의

IsAvailable 값이 변경될 때 발생합니다. 사용자가 디바이스 설정을 통해 디바이스에 대한 시스템 액세스를 변경할 때 발생할 수 있습니다.

이벤트 처리기의 매개 변수는 속성이 변경된 보낸 사람 LampArrayObject (항상 null임)입니다.

// Register
event_token AvailabilityChanged(TypedEventHandler<LampArray, IInspectable const&> const& handler) const;

// Revoke with event_token
void AvailabilityChanged(event_token const* cookie) const;

// Revoke with event_revoker
LampArray::AvailabilityChanged_revoker AvailabilityChanged(auto_revoke_t, TypedEventHandler<LampArray, IInspectable const&> const& handler) const;
/// [Windows.Foundation.Metadata.Experimental]
/// [add: Windows.Foundation.Metadata.Experimental]
/// [remove: Windows.Foundation.Metadata.Experimental]
// Register
event_token AvailabilityChanged(TypedEventHandler<LampArray, IInspectable const&> const& handler) const;

// Revoke with event_token
void AvailabilityChanged(event_token const* cookie) const;

// Revoke with event_revoker
LampArray::AvailabilityChanged_revoker AvailabilityChanged(auto_revoke_t, TypedEventHandler<LampArray, IInspectable const&> const& handler) const;
public event TypedEventHandler<LampArray,object> AvailabilityChanged;
[Windows.Foundation.Metadata.Experimental]
[add: Windows.Foundation.Metadata.Experimental]
[remove: Windows.Foundation.Metadata.Experimental]
public event TypedEventHandler<LampArray,object> AvailabilityChanged;
function onAvailabilityChanged(eventArgs) { /* Your code */ }
lampArray.addEventListener("availabilitychanged", onAvailabilityChanged);
lampArray.removeEventListener("availabilitychanged", onAvailabilityChanged);
- or -
lampArray.onavailabilitychanged = onAvailabilityChanged;
Public Custom Event AvailabilityChanged As TypedEventHandler(Of LampArray, Object) 

이벤트 유형

특성

Windows 요구 사항

디바이스 패밀리
Windows 11 Insider Preview (10.0.23504.0에서 도입되었습니다.)
API contract
Windows.Foundation.UniversalApiContract (v15.0에서 도입되었습니다.)

예제

LampArray 샘플

Windows.Devices.Lights 및 Windows.Devices.Lights.Effects API를 사용하여 주변 디바이스의 RGB 조명을 제어하는 방법을 보여 줍니다.

AutoRGB 샘플

데스크톱 화면에서 대표적인 단일 색을 추출하고 이를 사용하여 연결된 RGB 디바이스에서 LED 램프를 비추는 방법을 보여 줍니다.

AvailabilityChanged 이벤트 예제

다음 예제에서는 이벤트에 대 한 처리기를 설정 하는 방법을 보여 입니다 AvailabilityChanged . 코드는 처리기를 선언하기 전에 속성을 사용할 수 있는지 여부를 IsAvailable 먼저 확인하는 방법을 확인합니다.

using System;
using System.Collections.Generic;
using Windows.Devices.Enumeration;
using Windows.Devices.Lights;

namespace Sample
{
    class LampArrayHelper
    {
        private readonly DeviceWatcher watcher;
        private readonly Dictionary<string, LampArray> lampArrays;
        private readonly Dictionary<string, LampArrayEffectPlaylist> playlists;

        public LampArraySample()
        {
            lampArrays = new Dictionary<string, LampArray>();
            playlists = new Dictionary<string, LampArrayEffectPlaylist>();

            watcher = DeviceInformation.CreateWatcher(LampArray.GetDeviceSelector());

            watcher.Added += Watcher_Added;
            watcher.Removed += Watcher_Removed;

            watcher.Start();
        }

        private void DoEffectSetup(LampArray device)
        {
            Console.WriteLine("Starting effect playlist...");

            var indices = Enumerable.Range(0, device.LampCount).ToArray();

            var effect = new LampArraySolidEffect(device, indices)
            {
                Color = Colors.Red,
                CompletionBehavior = LampArrayEffectCompletionBehavior.KeepState,
                Duration = TimeSpan.FromMilliseconds(3000)
            };

            var effect2 = new LampArraySolidEffect(device, indices)
            {
                Color = Colors.Blue,
                CompletionBehavior = LampArrayEffectCompletionBehavior.KeepState,
                Duration = TimeSpan.FromMilliseconds(3000)
            };

            var playlist = new LampArrayEffectPlaylist();
            playlist.Append(effect);
            playlist.Append(effect2);
            playlist.Start();

            lock (playlists)
            {
                playlists.Add(device.DeviceId, playlist);
            }
        }

        private async void Watcher_Added(DeviceWatcher sender, DeviceInformation args)
        {
            var lampArray = await LampArray.FromIdAsync(args.Id);

            lock (lampArrays)
            {
                lampArrays[args.Id] = lampArray;
            }

            if (lampArray.LampArrayKind == LampArrayKind.Headset)
            {
                Console.WriteLine("Headset LampArray attached: " + lampArray.DeviceId);
            }

            if (ApiInformation.IsPropertyPresent("Windows.Devices.Lights.LampArray", "IsAvailable"))
            {
                if (lampArray.IsAvailable)
                {
                    DoEffectSetup(lampArray);
                }

                lampArray.AvailabilityChanged += LampArray_AvailabilityChanged;
            }
            else
            {
                DoEffectSetup(lampArray);
            }
        }

        private void Watcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
        {
            lock (lampArrays)
            {
                lampArrays.Remove(args.Id);
            }

            lock (playlists)
            {
                if (playlists.ContainsKey(args.Id))
                {
                    playlists[args.Id].Stop();
                    playlists.Remove(args.Id);
                }
            }
        }

        private void LampArray_AvailabilityChanged(LampArray sender, object args)
        {
            if (sender.IsAvailable)
            {
                Console.WriteLine("Device available: " + sender.DeviceId);
                DoEffectSetup(sender);
            }
            else
            {
                Console.WriteLine("Device unavailable: " + sender.DeviceId);

                lock (playlists)
                {
                    if (playlists.ContainsKey(sender.DeviceId))
                    {
                        playlists[sender.DeviceId].Stop();
                        playlists.Remove(sender.DeviceId);
                    }
                }
            }
        }
    }
}
#include <windows.h>

#include <algorithm>
#include <memory>
#include <numeric>
#include <vector>
#include <map>

#include <wil/resource.h>

#include <winrt/base.h>
#include <winrt/windows.devices.lights.h>
#include <winrt/windows.devices.lights.effects.h>
#include <winrt/windows.devices.enumeration.h>
#include <winrt/windows.foundation.h>
#include <winrt/windows.foundation.metadata.h>
#include <winrt/windows.ui.h>

namespace winrt
{
    using namespace winrt::Windows::Devices::Enumeration;
    using namespace winrt::Windows::Devices::Lights;
    using namespace winrt::Windows::Devices::Lights::Effects;
    using namespace winrt::Windows::Foundation;
    using namespace winrt::Windows::Foundation::Metadata;
    using namespace winrt::Windows::UI;
}

wil::srwlock lampArraysLock;
std::shared_ptr<std::map<winrt::hstring, winrt::LampArray>> lampArrays;

wil::srwlock playlistsLock;
std::shared_ptr<std::map<winrt::hstring, winrt::LampArrayEffectPlaylist>> playlists;

void DoEffectSetup(winrt::LampArray device)
{
    wprintf(L"Starting effect playlist...\n");

    std::vector<int32_t> indices(device.LampCount());
    std::iota(indices.begin(), indices.end(), 0);

    auto effect = winrt::LampArraySolidEffect(device, indices);
    effect.Color(winrt::Colors::Red());
    effect.CompletionBehavior(winrt::LampArrayEffectCompletionBehavior::KeepState);
    effect.Duration(std::chrono::milliseconds(3000));

    auto effect2 = winrt::LampArraySolidEffect(device, indices);
    effect2.Color(winrt::Colors::Blue());
    effect2.CompletionBehavior(winrt::LampArrayEffectCompletionBehavior::KeepState);
    effect2.Duration(std::chrono::milliseconds(3000));

    auto playlist = winrt::LampArrayEffectPlaylist();
    playlist.RepetitionMode(winrt::LampArrayRepetitionMode::Forever);

    playlist.Append(effect);
    playlist.Append(effect2);
    playlist.Start();

    auto lock = playlistsLock.lock_exclusive();
    playlists->emplace(std::make_pair(device.DeviceId(), playlist));
}

int __cdecl main()
{
    lampArrays = std::make_shared<std::map<winrt::hstring, winrt::LampArray>>();
    playlists = std::make_shared<std::map<winrt::hstring, winrt::LampArrayEffectPlaylist>>();

    auto deviceWatcher = winrt::DeviceInformation::CreateWatcher(winrt::LampArray::GetDeviceSelector());

    auto deviceAddedRevoker = deviceWatcher.Added(winrt::auto_revoke, [](winrt::DeviceWatcher, winrt::DeviceInformation info)
    {
        auto deviceId = info.Id();
        auto lampArray = winrt::LampArray::FromIdAsync(deviceId).get();

        if (lampArray)
        {
            wprintf(L"LampArray %s added\n", deviceId.c_str());

            {
                auto lock = lampArraysLock.lock_exclusive();
                lampArrays->emplace(std::make_pair(deviceId, lampArray));
            }

            if (winrt::ApiInformation::IsPropertyPresent(L"Windows.Devices.Lights.LampArray", L"IsAvailable"))
            {
                if (lampArray.IsAvailable())
                {
                    DoEffectSetup(lampArray);
                }

                lampArray.AvailabilityChanged([](winrt::LampArray device, winrt::IInspectable)
                {
                    if (device.IsAvailable())
                    {
                        wprintf(L"Device available: %s\n", device.DeviceId().c_str());
                        DoEffectSetup(device);
                    }
                    else
                    {
                        wprintf(L"Device unavailable: %s\n", device.DeviceId().c_str());

                        auto lock = playlistsLock.lock_exclusive();

                        auto entry = playlists->find(device.DeviceId());
                        if (entry != playlists->end())
                        {
                            entry->second.Stop();
                            playlists->erase(entry);
                        }
                    }
                });
            }
            else
            {
                DoEffectSetup(lampArray);
            }
        }
    });

    auto deviceRemovedRevoker = deviceWatcher.Removed(winrt::auto_revoke, [](winrt::DeviceWatcher, winrt::DeviceInformationUpdate info)
    {
        auto deviceId = info.Id();

        {
            auto lock = lampArraysLock.lock_exclusive();
            lampArrays->erase(deviceId);
        }

        {
            auto lock = playlistsLock.lock_exclusive();

            auto entry = playlists->find(deviceId);
            if (entry != playlists->end())
            {
                entry->second.Stop();
                playlists->erase(entry);
            }
        }

        wprintf(L"LampArray %s removed\n", deviceId.c_str());
    });

    deviceWatcher.Start();

    wprintf(L"Press any key to exit\n");
    getchar();

    deviceWatcher.Stop();

    return 0;
}

설명

포그라운드 및 백그라운드("앰비언트") 앱은 모두 이 이벤트를 수신하고 처리할 수 있습니다.

이 이벤트를 사용하려면 앱 매니페스트에서 "com.microsoft.windows.lighting" AppExtension을 선언해야 합니다. 이 작업을 수행하는 방법에 대한 자세한 내용은 앱 확장 만들기 및 호스트를 참조하세요.

<Extensions>
    <uap3:Extension Category="windows.appExtension">
        <uap3:AppExtension Name="com.microsoft.windows.lighting" Id="Id" DisplayName="Id">
        </uap3:AppExtension> 
    </uap3:Extension>
</Extensions>

적용 대상

추가 정보