none
Use ProjectionManager from .NET Framework RRS feed

  • Question

  • How can I use the UWP class ProjectionManager and call StartProjectingAsync to just start mirroring the current desktop.
    I don't want any
    new windows to pop up etc.
    Just duplicate PC1 screen to PC2 screen

    So it makes no sense to set 
    projectionViewId
    or
    anchorViewId

    Also it is not even possible to get teh windows ids from .NET 

    The sample in Windows-universal-samples-master.zip
    Doesn't really do what I want. I don't want to create any new windows etc.

    Just project the desktop nothing else.

    I want the projection PC2 to show exactly what is on PC1.

    This works fine if the projection is started from Windows with:

    start explorer.exe ms-settings-connectabledevices:devicediscovery

    But that means having to show Windows UI and have user select the projector etc.

    I want to do this automatically and show no windows UI and using .NET Framework

    So far I got this C# console app (see below) 

    It works but it means I have to ignore an exception:

    Name Value Type
    ex.Message "Catastrophic failure\r\n\r\nCatastrophic failure\r\n" string

    But the projection is still started and it works even if I pass in 0 and 0

    It is ugly but works.
    The console app will first get a list of all available projectors and the use the device info to call 
    StartProjectingAsync
    - Does anyone have a better solution?
    - Also it would be nice to be able to disconnect as well.

    /JD

    using System;
    using System.Threading.Tasks;
    using Windows.Devices.Enumeration;
    using Windows.UI.ViewManagement;

    namespace StartProj
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Pass in the name of the PC that we like to project to
                if(args.Length > 0)
                    GetProjector(args[0]).GetAwaiter().GetResult();           
            }
            private static async Task GetProjector(string name)
            {
                try
                {
                    //Get list of available projectors
                    var projectorSelectorQuery = ProjectionManager.GetDeviceSelector();
                    var outputDevices = await DeviceInformation.FindAllAsync(projectorSelectorQuery);
     
                    // Loop through all devices and find match
                    for (int i = 0; i < outputDevices.Count; i++)
                    {
                        var device = outputDevices[i];
                        if(device.Name == name)
                        {
                            try
                            {
                                //Start projector for device
                                await ProjectionManager.StartProjectingAsync(0, 0, device);
                            }
                            catch(Exception ex)
                            {
                                //Ignore this exception
                                Console.WriteLine(ex.Message);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
    }
    Friday, October 18, 2019 3:19 PM

All replies

  • Hi,

    Since you are developing a desktop app, so it will be more suitable to ask about this in C# forum. I'll help you to move this thread to C# forum and you can get more help there.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 21, 2019 1:26 AM
  • Yes,please help me move to the correct place
    Monday, October 21, 2019 5:04 PM
  • Hi Johan,

    Thank you for posting here.

    I tried to add Windows.Devices.Enumeration and Windows.UI.ViewManagement to the Console App but I failed.

    According to your description, you seem to have successfully done this, so could you tell me how to do this so that I can reproduce your mistakes?

    Looking forward to your reply.

    Best regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 22, 2019 8:55 AM
  • All I done is:
    1. Create a new Console Application for .NET 4.7.2 in visual Studio 2019

    2. Add referenses to:
        <Reference Include="System" />
        <Reference Include="System.Core" />
        <Reference Include="System.Runtime.InteropServices.WindowsRuntime" />
        <Reference Include="System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
          <SpecificVersion>False</SpecificVersion>
          <HintPath>..\..\..\Uwp\System.Runtime.WindowsRuntime.dll</HintPath>
        </Reference>
        <Reference Include="System.Xml.Linq" />
        <Reference Include="System.Data.DataSetExtensions" />
        <Reference Include="Microsoft.CSharp" />
        <Reference Include="System.Data" />
        <Reference Include="System.Net.Http" />
        <Reference Include="System.Xml" />
        <Reference Include="Windows">
          <HintPath>..\..\..\Uwp\Windows.WinMD</HintPath>
        </Reference>
        <Reference Include="Windows.Foundation.FoundationContract">
          <HintPath>..\..\..\Uwp\Windows.Foundation.FoundationContract.winmd</HintPath>
        </Reference>
        <Reference Include="Windows.Foundation.UniversalApiContract">
          <HintPath>..\..\..\Uwp\Windows.Foundation.UniversalApiContract.winmd</HintPath>
        </Reference>
        <Reference Include="WindowsBase" />

    3. Add code to: class Program

    ===============================

    using System;
    using System.Threading.Tasks;
    using Windows.Devices.Enumeration;
    using Windows.UI.ViewManagement;
    namespace StartProj
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Pass in the name of the PC that we like to project to
                if(args.Length > 0)
                    GetProjector(args[0]).GetAwaiter().GetResult();           
            }
            private static async Task GetProjector(string name)
            {
                try
                {
                    //Get list of available projectors
                    var projectorSelectorQuery = ProjectionManager.GetDeviceSelector();
                    var outputDevices = await DeviceInformation.FindAllAsync(projectorSelectorQuery);
     
                    // Loop through all devices and find match
                    for (int i = 0; i < outputDevices.Count; i++)
                    {
                        var device = outputDevices[i];
                        if(device.Name == name)
                        {
                            try
                            {
                                //Start projector for device
                                await ProjectionManager.StartProjectingAsync(0, 0, device);
                            }
                            catch(Exception ex)
                            {
                                //Ignore this exception
                                Console.WriteLine(ex.Message);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }


    ===============================

    4. Debug Any CPU

    5. Make sure the name of the command line argument matches the projector name

    6. The exception will occur after calling:
                                await ProjectionManager.StartProjectingAsync(0, 0, device);

    7. The exception message says: "
    Catastrophic failure"




    Tuesday, October 22, 2019 7:06 PM
  • Hi Johan,

    Thanks for the feedback.

    Could you tell me about where you add the reference, is app.config or other places?

    We will be appreciated if you told me about it.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 24, 2019 8:12 AM