UWP C# 将设备信息保存到 ApplicationData

UWP C# Saving DeviceInformation to ApplicationData

我正在尝试在我的应用程序中保存 device information。 我设法将 as string 保存到 ApplicationData.Current.RoamingSettings,但无法保存为 deviceinformation,我需要在应用程序启动时使用它来枚举我的设备。

我不确定这样做的正确方法是什么。 有人可以建议吗? 谢谢。

    const string MicDeviceNameKey = "MicDeviceName";
    const string MicDeviceIDKey = "MicDeviceID";
    IPropertySet settings = ApplicationData.Current.RoamingSettings.Values;

    public string MicDeviceName
    {
        get
        {
            object setting;
            if (settings.TryGetValue(MicDeviceNameKey, out setting))
                return (string)setting;
            return null;
        }
        set
        {
            settings[MicDeviceNameKey] = value;
        }
    }

    public DeviceInformation MicDeviceID
    {
        get
        {
            if (settings[MicDeviceIDKey] != null)
                return (DeviceInformation)settings[MicDeviceIDKey];
            return null;
        }
        set
        {
            settings[MicDeviceIDKey] = value;
        }
    }

已更新

我正在使用 AudioGraph 将麦克风输入路由到线路输出。 无论如何,我可以在哪里加载 CaptureRender 设备的已保存 Device Information 来初始化 AudioGraphSettings.

public async Task CreateMicRoute(DeviceInformation output, DeviceInformation input)
    {
        // ################################################################################################
        // Register the Output Source for the Playback
        var settings = new AudioGraphSettings(AudioRenderCategory.Media);
        settings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.LowestLatency;
        settings.PrimaryRenderDevice = output;

        var result = await AudioGraph.CreateAsync(settings);

        if (result.Status != AudioGraphCreationStatus.Success)
        {
            // Cannot create graph
            //rootPage.NotifyUser(String.Format("AudioGraph Creation Error because {0}", result.Status.ToString()), NotifyType.ErrorMessage);
            return;
        }

        //Create Audio Graph
        micAudioGraph = result.Graph;

        // ################################################################################################
        // Create a device input node
        var inProfile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Medium);
        var DeviceInputResult = await micAudioGraph.CreateDeviceInputNodeAsync(MediaCategory.Media, inProfile.Audio, input);
        if (DeviceInputResult.Status != AudioDeviceNodeCreationStatus.Success)
        {
            // Cannot create device input node
            return;
        }

        // Set the Created Audio Input Node to Device Input Node
        deviceInputNode = DeviceInputResult.DeviceInputNode;

        // Create a device output node
        var DeviceOutputResult = await micAudioGraph.CreateDeviceOutputNodeAsync();
        if (DeviceOutputResult.Status != AudioDeviceNodeCreationStatus.Success)
        {
            // Cannot create device output node
            return ;
        }
        // Selected Output device from Audio Graph is set to Device Output Node
        deviceOutputNode = DeviceOutputResult.DeviceOutputNode;

        // ################################################################################################           
        if (micAudioGraph == null)
            return;
    }

请检查文档 here,

For both LocalSettings and RoamingSettings, the name of each setting can be 255 characters in length at most. Each setting can be up to 8K bytes in size and each composite setting can be up to 64K bytes in size.

Windows Runtime data types are supported 应用程序设置。但是 DeviceInformation 不在支持列表中。对于您的场景,我们建议您保存一些关于 DeviceInformation 此类设备 ID 设备类型的键值。并获取具有设备 ID 的设备。

更新

DeviceInformation 包含 CreateFromIdAsync 方法,您可以将 DeviceInformation 的 id 存储到本地设置,并使用以下代码检索 DeviceInformation

DeviceInformation.CreateFromIdAsync("Device ID");