简体   繁体   中英

UWP C# Saving DeviceInformation to ApplicationData

I am trying to save device information in my application. I managed to save as string to ApplicationData.Current.RoamingSettings but unable to save as deviceinformation which I need to use to enumerate my device when the app starts.

I am not sure what is the right approach to do so. Can somebody advise? Thanks.

    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;
        }
    }

在此处输入图像描述

Updated

I am using AudioGraph to route mic input to line output. Is there anyway to work around where I can load saved Device Information for Capture and Render device to initialize 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;
    }

Please check document 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.

The Windows Runtime data types are supported for app settings. But DeviceInformation is not in the support list. For your scenario, we suggest your save some key value about DeviceInformation such device id device kind. and get the device with device id.

Update

DeviceInformation contains CreateFromIdAsync method, you could store the DeviceInformation's id in to local setting, and retrieve DeviceInformation with the following code.

DeviceInformation.CreateFromIdAsync("Device ID");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM