Example #1
0
        private async void Picker_DialDeviceSelected(DialDevicePicker sender, DialDeviceSelectedEventArgs args)
        {
            //Casting must occur from the UI thread.  This dispatches the casting calls to the UI thread.
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                // The args.SelectedCastingDevice is proxied from the picker process. The picker process is
                // dismissmed as soon as you break into the debugger. Creating a non-proxied version
                // allows debugging since the proxied version stops working once the picker is dismissed.
                DialDevice selectedDevice = await DialDevice.FromIdAsync(args.SelectedDialDevice.Id);

                // Set the status to connecting
                try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Connecting); } catch { }

                // Get the DeviceInformation instance for the the selected device
                DeviceInformation selectedDeviceInfo = await DeviceInformation.CreateFromIdAsync(selectedDevice.Id);

                //Get the DialApp object for the specific application on the selected device
                DialApp app = selectedDevice.GetDialApp(this.dial_appname_textbox.Text);

                if (app == null)
                {
                    //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL.
                    rootPage.NotifyUser(string.Format("'{0}' cannot find app with ID '{1}'", selectedDeviceInfo.Name, this.dial_appname_textbox.Text), NotifyType.StatusMessage);
                    try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Error); } catch { }
                }
                else
                {
                    //Get the current application state
                    DialAppStateDetails stateDetails = await app.GetAppStateAsync();
                    if (stateDetails.State == DialAppState.NetworkFailure)
                    {
                        // In case getting the application state failed because of a network failure
                        rootPage.NotifyUser("Network Failure while getting application state", NotifyType.ErrorMessage);
                        try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Error); } catch { }
                    }
                    else
                    {
                        rootPage.NotifyUser(string.Format("Attempting to launch '{0}'", app.AppName), NotifyType.StatusMessage);
                        //Launch the application on the 1st screen device
                        DialAppLaunchResult result = await app.RequestLaunchAsync(this.dial_launch_args_textbox.Text);

                        //Verify to see whether the application was launched
                        if (result == DialAppLaunchResult.Launched)
                        {
                            rootPage.NotifyUser(string.Format("Launched '{0}'", app.AppName), NotifyType.StatusMessage);
                            //This is where you will need to add you application specific communication between your 1st and 2nd screen applications
                            //...
                            try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Connected); } catch { }
                        }
                        else
                        {
                            rootPage.NotifyUser(string.Format("Attempting to launch '{0}'", app.AppName), NotifyType.StatusMessage);
                            try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Error); } catch { }
                        }
                    }
                }
            });
        }
Example #2
0
        private async Task <bool> TryStopDialAppAsync(DialApp app)
        {
            bool stopped = false;

            //Get the current application state
            DialAppStateDetails stateDetails = await app.GetAppStateAsync();

            switch (stateDetails.State)
            {
            case DialAppState.NetworkFailure:
            {
                // In case getting the application state failed because of a network failure, you could add retry logic
                rootPage.NotifyUser("Network Failure while getting application state", NotifyType.ErrorMessage);
                break;
            }

            case DialAppState.Stopped:
            {
                stopped = true;
                // In case getting the application state failed because of a network failure, you could add retry logic
                rootPage.NotifyUser("Application was already stopped.", NotifyType.StatusMessage);
                break;
            }

            default:
            {
                DialAppStopResult result = await app.StopAsync();

                if (result == DialAppStopResult.Stopped)
                {
                    stopped = true;
                    // In case getting the application state failed because of a network failure, you could add retry logic
                    rootPage.NotifyUser("Application stopped successfully.", NotifyType.StatusMessage);
                }
                else
                {
                    if (result == DialAppStopResult.StopFailed || result == DialAppStopResult.NetworkFailure)
                    {
                        // In case getting the application state failed because of a network failure, you could add retry logic
                        rootPage.NotifyUser(string.Format("Error occured trying to stop application. Status: '{0}'", result.ToString()), NotifyType.StatusMessage);
                    }
                    else         //in case of DialAppStopResult.OperationNotSupported, there is not much more you can do. You could implement your own
                    // mechanism to stop the application on that device.
                    {
                        stopped = true;
                        // In case getting the application state failed because of a network failure, you could add retry logic
                        rootPage.NotifyUser(string.Format("Stop is not supported by device: '{0}'", activeDevice.Name), NotifyType.ErrorMessage);
                    }
                }
                break;
            }
            }
            return(stopped);
        }
Example #3
0
        private async Task <bool> TryLaunchDialAppAsync(DeviceInformation device)
        {
            bool dialAppLaunchSucceeded = false;

            if (device.Id.IndexOf("dial", StringComparison.OrdinalIgnoreCase) > -1)
            {
                //Update the launch arguments to include the Position
                this.dial_launch_args_textbox.Text = string.Format("v={0}&t={1}&pairingCode=E4A8136D-BCD3-45F4-8E49-AE01E9A46B5F", video.Id, player.Position.Ticks);

                //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL.
                rootPage.NotifyUser(string.Format("Checking to see if device {0} supports DIAL", device.Name), NotifyType.StatusMessage);
                //BUG: Takes too long. Workaround, just try to create the DialDevice
                //if (await DialDevice.DeviceInfoSupportsDialAsync(device))
                //{
                DialDevice dialDevice = null;

                //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL.
                rootPage.NotifyUser(string.Format("Attempting to resolve DIAL device for '{0}'", device.Name), NotifyType.StatusMessage);
                try { dialDevice = await DialDevice.FromIdAsync(device.Id); } catch { }

                if (dialDevice == null)
                {
                    //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL.
                    rootPage.NotifyUser(string.Format("'{0}' does not support DIAL", device.Name), NotifyType.StatusMessage);
                }
                else
                {
                    //Get the DialApp object for the specific application on the selected device
                    DialApp app = dialDevice.GetDialApp(this.dial_appname_textbox.Text);

                    if (app == null)
                    {
                        //Try to create a DIAL device. If it doesn't succeed, the selected device does not support DIAL.
                        rootPage.NotifyUser(string.Format("'{0}' cannot find app with ID '{1}'", device.Name, this.dial_appname_textbox.Text), NotifyType.StatusMessage);
                    }
                    else
                    {
                        //Get the current application state
                        DialAppStateDetails stateDetails = await app.GetAppStateAsync();

                        if (stateDetails.State == DialAppState.NetworkFailure)
                        {
                            // In case getting the application state failed because of a network failure
                            rootPage.NotifyUser("Network Failure while getting application state", NotifyType.ErrorMessage);
                        }
                        else
                        {
                            rootPage.NotifyUser(string.Format("Attempting to launch '{0}'", app.AppName), NotifyType.StatusMessage);
                            //Launch the application on the 1st screen device
                            DialAppLaunchResult result = await app.RequestLaunchAsync(this.dial_launch_args_textbox.Text);

                            //Verify to see whether the application was launched
                            if (result == DialAppLaunchResult.Launched)
                            {
                                //Remember the device to which casting succeeded
                                activeDevice = device;
                                //DIAL is sessionsless but the DIAL app allows us to get the state and "disconnect".
                                //Disconnect in the case of DIAL is equivalenet to stopping the app.
                                activeCastConnectionHandler = app;
                                rootPage.NotifyUser(string.Format("Launched '{0}'", app.AppName), NotifyType.StatusMessage);
                                //This is where you will need to add you application specific communication between your 1st and 2nd screen applications
                                //...
                                dialAppLaunchSucceeded = true;
                            }
                        }
                    }
                }
                //}
                //else
                //{
                //    rootPage.NotifyUser(string.Format("'{0}' does not support DIAL", device.Name), NotifyType.StatusMessage);
                //}
            }
            else
            {
                rootPage.NotifyUser(string.Format("'{0}' does not support DIAL", device.Name), NotifyType.StatusMessage);
            }
            return(dialAppLaunchSucceeded);
        }
Example #4
0
        private async void Picker_DisconnectButtonClicked(DialDevicePicker sender, DialDisconnectButtonClickedEventArgs args)
        {
            //Casting must occur from the UI thread.  This dispatches the casting calls to the UI thread.
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                // The args.SelectedCastingDevice is proxied from the picker process. The picker process is
                // dismissmed as soon as you break into the debugger. Creating a non-proxied version
                // allows debugging since the proxied version stops working once the picker is dismissed.
                DialDevice selectedDevice = await DialDevice.FromIdAsync(args.Device.Id);

                // Get the DeviceInformation instance for the the selected device
                DeviceInformation selectedDeviceInfo = await DeviceInformation.CreateFromIdAsync(selectedDevice.Id);

                // Update the picker status
                try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Disconnecting); } catch { }

                DialApp app = args.Device.GetDialApp(this.dial_appname_textbox.Text);

                //Get the current application state
                DialAppStateDetails stateDetails = await app.GetAppStateAsync();

                switch (stateDetails.State)
                {
                case DialAppState.NetworkFailure:
                    {
                        // In case getting the application state failed because of a network failure, you could add retry logic
                        rootPage.NotifyUser("Network Failure while getting application state", NotifyType.ErrorMessage);
                        // Update the picker status
                        try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Error); } catch { }
                        break;
                    }

                case DialAppState.Stopped:
                    {
                        // In case getting the application state failed because of a network failure, you could add retry logic
                        rootPage.NotifyUser("Application was already stopped.", NotifyType.StatusMessage);
                        try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Disconnected); } catch { }
                        break;
                    }

                default:
                    {
                        DialAppStopResult result = await app.StopAsync();

                        if (result == DialAppStopResult.Stopped)
                        {
                            // In case getting the application state failed because of a network failure, you could add retry logic
                            rootPage.NotifyUser("Application stopped successfully.", NotifyType.StatusMessage);
                            try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Disconnected); } catch { }
                        }
                        else
                        {
                            if (result == DialAppStopResult.StopFailed || result == DialAppStopResult.NetworkFailure)
                            {
                                // In case getting the application state failed because of a network failure, you could add retry logic
                                rootPage.NotifyUser(string.Format("Error occured trying to stop application. Status: '{0}'", result.ToString()), NotifyType.StatusMessage);
                                try { picker.SetDisplayStatus(selectedDevice, DialDeviceDisplayStatus.Error); } catch { }
                            }
                            else     //in case of DialAppStopResult.OperationNotSupported, there is not much more you can do. You could implement your own
                                     // mechanism to stop the application on that device.
                            {
                                // In case getting the application state failed because of a network failure, you could add retry logic
                                rootPage.NotifyUser(string.Format("Stop is not supported by device: '{0}'", selectedDeviceInfo.Name), NotifyType.ErrorMessage);
                            }
                        }
                        break;
                    }
                }
            });
        }